Search in sources :

Example 71 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project commons-dbcp by apache.

the class TestCPDSConnectionFactory method testConnectionErrorCleanup.

/**
 * JIRA DBCP-216
 *
 * Verify that pool counters are maintained properly and listeners are
 * cleaned up when a PooledConnection throws a connectionError event.
 */
@Test
public void testConnectionErrorCleanup() throws Exception {
    // Setup factory
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, null, Duration.ofMillis(-1), false, "userName", "password");
    try (final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory)) {
        factory.setPool(pool);
        // Checkout a pair of connections
        final PooledConnection pcon1 = pool.borrowObject().getPooledConnection();
        try (final Connection con1 = pcon1.getConnection()) {
            final PooledConnection pcon2 = pool.borrowObject().getPooledConnection();
            assertEquals(2, pool.getNumActive());
            assertEquals(0, pool.getNumIdle());
            // Verify listening
            final PooledConnectionProxy pc = (PooledConnectionProxy) pcon1;
            assertTrue(pc.getListeners().contains(factory));
            // Throw connectionError event
            pc.throwConnectionError();
            // Active count should be reduced by 1 and no idle increase
            assertEquals(1, pool.getNumActive());
            assertEquals(0, pool.getNumIdle());
            // Throw another one - should be ignored
            pc.throwConnectionError();
            assertEquals(1, pool.getNumActive());
            assertEquals(0, pool.getNumIdle());
            // Ask for another connection
            final PooledConnection pcon3 = pool.borrowObject().getPooledConnection();
            // better not get baddie back
            assertNotEquals(pcon3, pcon1);
            // verify cleanup
            assertFalse(pc.getListeners().contains(factory));
            assertEquals(2, pool.getNumActive());
            assertEquals(0, pool.getNumIdle());
            // Return good connections back to pool
            pcon2.getConnection().close();
            pcon3.getConnection().close();
            assertEquals(2, pool.getNumIdle());
            assertEquals(0, pool.getNumActive());
            // Verify pc is closed
            assertThrows(SQLException.class, pc::getConnection, "Expecting SQLException using closed PooledConnection");
            // Back from the dead - ignore the ghost!
            con1.close();
            assertEquals(2, pool.getNumIdle());
            assertEquals(0, pool.getNumActive());
            // Clear pool
            pool.clear();
            assertEquals(0, pool.getNumIdle());
        }
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.jupiter.api.Test)

Example 72 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project commons-dbcp by apache.

the class TestCPDSConnectionFactory method testNullValidationQuery.

/**
 * JIRA: DBCP-442
 */
@Test
public void testNullValidationQuery() throws Exception {
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, null, Duration.ofMillis(-1), false, "userName", "password");
    try (final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory)) {
        factory.setPool(pool);
        pool.setTestOnBorrow(true);
        final PooledConnection pcon = pool.borrowObject().getPooledConnection();
        try (final Connection con = pcon.getConnection()) {
        }
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.jupiter.api.Test)

Example 73 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project commons-dbcp by apache.

the class TestPoolingDataSource method testFixFactoryConfig.

/**
 * DBCP-412
 * Verify that omitting factory.setPool(pool) when setting up PDS does not
 * result in NPE.
 */
@Test
public void testFixFactoryConfig() throws Exception {
    final Properties properties = new Properties();
    properties.setProperty(Constants.KEY_USER, "userName");
    properties.setProperty(Constants.KEY_PASSWORD, "password");
    final PoolableConnectionFactory f = new PoolableConnectionFactory(new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties), null);
    f.setValidationQuery("SELECT DUMMY FROM DUAL");
    f.setDefaultReadOnly(Boolean.TRUE);
    f.setDefaultAutoCommit(Boolean.TRUE);
    final GenericObjectPool<PoolableConnection> p = new GenericObjectPool<>(f);
    p.setMaxTotal(getMaxTotal());
    p.setMaxWait(getMaxWaitDuration());
    ds = new PoolingDataSource<>(p);
    assertEquals(f.getPool(), p);
    ds.getConnection();
}
Also used : Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.jupiter.api.Test)

Example 74 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project commons-dbcp by apache.

the class TestPoolingDriver method test1.

@Test
public void test1() {
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "userName", "password");
    final PoolableConnectionFactory pcf = new PoolableConnectionFactory(connectionFactory, null);
    pcf.setDefaultReadOnly(Boolean.FALSE);
    pcf.setDefaultAutoCommit(Boolean.TRUE);
    final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(pcf);
    pcf.setPool(connectionPool);
    final DataSource ds = new PoolingDataSource<>(connectionPool);
    Assertions.assertNotNull(ds);
}
Also used : GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 75 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project jade-data-repo by DataBiosphere.

the class JdbcConfiguration method configureDataSource.

private void configureDataSource() {
    Properties props = new Properties();
    props.setProperty("user", getUsername());
    props.setProperty("password", getPassword());
    props.setProperty("maxTotal", String.valueOf(poolMaxTotal));
    props.setProperty("maxIdle", String.valueOf(poolMaxIdle));
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(getUri(), props);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    dataSource = new PoolingDataSource<>(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2