Search in sources :

Example 11 with GenericObjectPool

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

the class TestCPDSConnectionFactory method testNullValidationQuery_Deprecated.

/**
 * JIRA: DBCP-442
 */
@Test
public void testNullValidationQuery_Deprecated() throws Exception {
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, null, -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 12 with GenericObjectPool

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

the class TestSynchronizationOrder method testSessionSynchronization.

@Test
public void testSessionSynchronization() throws Exception {
    final DataSourceXAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(transactionManager, xads);
    final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);
    // create the pool
    try (final GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(factory)) {
        factory.setPool(pool);
        pool.setMaxTotal(10);
        pool.setMaxWait(Duration.ofSeconds(1));
        // finally create the datasource
        try (final ManagedDataSource<PoolableConnection> ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry())) {
            ds.setAccessToUnderlyingConnectionAllowed(true);
            transactionManager.begin();
            try (final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) ds.getConnection()) {
            // close right away.
            }
            transactionManager.commit();
            assertTrue(transactionManagerRegistered);
            assertFalse(transactionSynchronizationRegistryRegistered);
        }
    }
}
Also used : DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 13 with GenericObjectPool

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

the class TestSynchronizationOrder method testInterposedSynchronization.

@Test
public void testInterposedSynchronization() throws Exception {
    final DataSourceXAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(transactionManager, xads, transactionSynchronizationRegistry);
    final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);
    // create the pool
    try (final GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(factory)) {
        factory.setPool(pool);
        pool.setMaxTotal(10);
        pool.setMaxWait(Duration.ofSeconds(1));
        // finally create the datasource
        try (final ManagedDataSource<PoolableConnection> ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry())) {
            ds.setAccessToUnderlyingConnectionAllowed(true);
            transactionManager.begin();
            try (final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) ds.getConnection()) {
            // Close right away.
            }
            transactionManager.commit();
            assertFalse(transactionManagerRegistered);
            assertTrue(transactionSynchronizationRegistryRegistered);
        }
    }
}
Also used : DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 14 with GenericObjectPool

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

the class PoolingDataSourceExample method setupDataSource.

public static DataSource setupDataSource(String connectURI) {
    // 
    // First, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    // 
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);
    // 
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    // 
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // 
    // Now we'll need a ObjectPool that serves as the
    // actual pool of connections.
    // 
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    // 
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);
    // 
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    // 
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    return dataSource;
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 15 with GenericObjectPool

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

the class PoolingDriverExample method setupDriver.

public static void setupDriver(String connectURI) throws Exception {
    // 
    // First, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    // 
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);
    // 
    // Next, we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    // 
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // 
    // Now we'll need a ObjectPool that serves as the
    // actual pool of connections.
    // 
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    // 
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);
    // 
    // Finally, we create the PoolingDriver itself...
    // 
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    // 
    // ...and register our pool with it.
    // 
    driver.registerPool("example", connectionPool);
// 
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
// 
}
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) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) 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