Search in sources :

Example 66 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project datanucleus-rdbms by datanucleus.

the class PerUserPoolDataSource method registerPool.

private synchronized void registerPool(final String userName, final String password) throws NamingException, SQLException {
    final ConnectionPoolDataSource cpds = testCPDS(userName, password);
    // Set up the factory we will use (passing the pool associates
    // the factory with the pool, so we do not have to do so
    // explicitly)
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password);
    factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
    // Create an object pool to contain our PooledConnections
    final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
    pool.setEvictionPolicyClassName(getPerUserEvictionPolicyClassName(userName));
    pool.setLifo(getPerUserLifo(userName));
    pool.setMaxIdle(getPerUserMaxIdle(userName));
    pool.setMaxTotal(getPerUserMaxTotal(userName));
    pool.setMaxWaitMillis(getPerUserMaxWaitMillis(userName));
    pool.setMinEvictableIdleTimeMillis(getPerUserMinEvictableIdleTimeMillis(userName));
    pool.setMinIdle(getPerUserMinIdle(userName));
    pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
    pool.setSoftMinEvictableIdleTimeMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName));
    pool.setTestOnCreate(getPerUserTestOnCreate(userName));
    pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
    pool.setTestOnReturn(getPerUserTestOnReturn(userName));
    pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
    pool.setTimeBetweenEvictionRunsMillis(getPerUserTimeBetweenEvictionRunsMillis(userName));
    pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
    final Object old = managers.put(getPoolKey(userName), factory);
    if (old != null) {
        throw new IllegalStateException("Pool already contains an entry for this user/password: " + userName);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) SwallowedExceptionLogger(org.datanucleus.store.rdbms.datasource.dbcp2.SwallowedExceptionLogger) GenericObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool)

Example 67 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project athenz by AthenZ.

the class DataSourceFactory method create.

static PoolableDataSource create(ConnectionFactory connectionFactory) {
    // setup our pool config object
    GenericObjectPoolConfig config = setupPoolConfig();
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // Set max lifetime of a connection in milli-secs, after which it will
    // always fail activation, passivation, and validation.
    // Value of -1 means infinite life time. The default value
    // defined in this class is 10 minutes.
    long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
    poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
    if (LOG.isInfoEnabled()) {
        LOG.info("Setting Time-To-Live interval for live connections ({}) msecs", connTtlMillis);
    }
    // set the validation query for our jdbc connector
    final String validationQuery = System.getProperty(ATHENZ_PROP_DBPOOL_VALIDATION_QUERY, MYSQL_VALIDATION_QUERY);
    poolableConnectionFactory.setValidationQuery(validationQuery);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    return new AthenzDataSource(connectionPool);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 68 with GenericObjectPool

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

the class TestDriverManagerConnectionFactory method testDriverManagerInit.

public void testDriverManagerInit(final boolean withProperties) throws Exception {
    final GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(10);
    config.setMaxIdle(0);
    final Properties properties = new Properties();
    // The names "user" and "password" are specified in java.sql.DriverManager.getConnection(String, String, String)
    properties.setProperty(Constants.KEY_USER, "foo");
    properties.setProperty(Constants.KEY_PASSWORD, "bar");
    final ConnectionFactory connectionFactory = withProperties ? new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", properties) : new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", "foo", "bar");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    poolableConnectionFactory.setDefaultReadOnly(Boolean.FALSE);
    poolableConnectionFactory.setDefaultAutoCommit(Boolean.TRUE);
    final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    final PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    final ConnectionThread[] connectionThreads = new ConnectionThread[10];
    final Thread[] threads = new Thread[10];
    for (int i = 0; i < 10; i++) {
        connectionThreads[i] = new ConnectionThread(dataSource);
        threads[i] = new Thread(connectionThreads[i]);
    }
    for (int i = 0; i < 10; i++) {
        threads[i].start();
    }
    for (int i = 0; i < 10; i++) {
        while (threads[i].isAlive()) {
            // JDK1.5: getState() != Thread.State.TERMINATED) {
            Thread.sleep(100);
        }
        if (!connectionThreads[i].getResult()) {
            fail("Exception during getConnection(): " + connectionThreads[i]);
        }
    }
}
Also used : Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)

Example 69 with GenericObjectPool

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

the class TestPStmtPooling method testBatchUpdate.

@Test
public void testBatchUpdate() throws Exception {
    DriverManager.registerDriver(new TesterDriver());
    final ConnectionFactory connFactory = new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", "u1", "p1");
    final PoolableConnectionFactory pcf = new PoolableConnectionFactory(connFactory, null);
    pcf.setPoolStatements(true);
    pcf.setDefaultReadOnly(Boolean.FALSE);
    pcf.setDefaultAutoCommit(Boolean.TRUE);
    final ObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(pcf);
    pcf.setPool(connPool);
    final PoolingDataSource<?> ds = new PoolingDataSource<>(connPool);
    final Connection conn = ds.getConnection();
    final PreparedStatement ps = conn.prepareStatement("select 1 from dual");
    final Statement inner = ((DelegatingPreparedStatement) ps).getInnermostDelegate();
    // Check DBCP-372
    ps.addBatch();
    ps.close();
    conn.close();
    Assertions.assertFalse(inner.isClosed());
    ds.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.jupiter.api.Test)

Example 70 with GenericObjectPool

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

the class TestPStmtPooling method testCallableStatementPooling.

@Test
public void testCallableStatementPooling() throws Exception {
    DriverManager.registerDriver(new TesterDriver());
    final ConnectionFactory connFactory = new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", "u1", "p1");
    final ObjectName oName = new ObjectName("UnitTests:DataSource=test");
    final PoolableConnectionFactory pcf = new PoolableConnectionFactory(connFactory, oName);
    pcf.setPoolStatements(true);
    pcf.setDefaultReadOnly(Boolean.FALSE);
    pcf.setDefaultAutoCommit(Boolean.TRUE);
    final GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
    config.setJmxNameBase("UnitTests:DataSource=test,connectionpool=connections");
    config.setJmxNamePrefix("");
    final ObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(pcf, config);
    pcf.setPool(connPool);
    final PoolingDataSource<?> ds = new PoolingDataSource<>(connPool);
    try (Connection conn = ds.getConnection()) {
        final Statement stmt1 = conn.prepareStatement("select 1 from dual");
        final Statement ustmt1 = ((DelegatingStatement) stmt1).getInnermostDelegate();
        final Statement cstmt1 = conn.prepareCall("{call home}");
        final Statement ucstmt1 = ((DelegatingStatement) cstmt1).getInnermostDelegate();
        // Return to pool
        stmt1.close();
        // ""
        cstmt1.close();
        // Check out from pool
        final Statement stmt2 = conn.prepareStatement("select 1 from dual");
        final Statement ustmt2 = ((DelegatingStatement) stmt2).getInnermostDelegate();
        final Statement cstmt2 = conn.prepareCall("{call home}");
        final Statement ucstmt2 = ((DelegatingStatement) cstmt2).getInnermostDelegate();
        // Return to pool
        stmt2.close();
        // ""
        cstmt2.close();
        assertSame(ustmt1, ustmt2);
        assertSame(ucstmt1, ucstmt2);
        // Verify key distinguishes Callable from Prepared Statements in the pool
        final Statement stmt3 = conn.prepareCall("select 1 from dual");
        final Statement ustmt3 = ((DelegatingStatement) stmt3).getInnermostDelegate();
        stmt3.close();
        assertNotSame(ustmt1, ustmt3);
        assertNotSame(ustmt3, ucstmt1);
    }
    ds.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) ObjectName(javax.management.ObjectName) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) Test(org.junit.jupiter.api.Test)

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