Search in sources :

Example 6 with GenericObjectPool

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

the class TestPStmtPooling method testClosePool.

@Test
public void testClosePool() 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);
    ((PoolingDataSource<?>) ds).setAccessToUnderlyingConnectionAllowed(true);
    final Connection conn = ds.getConnection();
    try (Statement s = conn.prepareStatement("select 1 from dual")) {
    }
    final Connection poolableConnection = ((DelegatingConnection<?>) conn).getDelegate();
    final Connection poolingConnection = ((DelegatingConnection<?>) poolableConnection).getDelegate();
    poolingConnection.close();
    try (PreparedStatement ps = conn.prepareStatement("select 1 from dual")) {
        fail("Expecting SQLException");
    } catch (final SQLException ex) {
        assertTrue(ex.getMessage().endsWith("invalid PoolingConnection."));
    }
    ds.close();
}
Also used : SQLException(java.sql.SQLException) 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 7 with GenericObjectPool

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

the class TestPoolingDataSource method testClose.

@Test
public void testClose() 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());
    try (PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(p)) {
        final Connection connection = dataSource.getConnection();
        assertNotNull(connection);
        connection.close();
    }
    assertTrue(p.isClosed());
    assertEquals(0, p.getNumIdle());
    assertEquals(0, p.getNumActive());
}
Also used : Connection(java.sql.Connection) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.jupiter.api.Test)

Example 8 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project interlok by adaptris.

the class WorkflowWithObjectPoolTest method testPopulatePool_FailToInit.

@Test(expected = CoreException.class)
public void testPopulatePool_FailToInit() throws Exception {
    setMinIdle(10);
    setServiceCollection(new ServiceList(new MockService(MockService.FailureCondition.Lifecycle)));
    setInitWaitTime(new TimeInterval(1L, TimeUnit.SECONDS));
    GenericObjectPool<Worker> pool = (GenericObjectPool<Worker>) createObjectPool();
    try {
        populatePool(pool);
    } finally {
        Closer.closeQuietly(pool);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockService(com.adaptris.core.stubs.MockService) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.Test)

Example 9 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project interlok by adaptris.

the class WorkflowWithObjectPoolTest method testPopulatePool.

@Test
public void testPopulatePool() throws Exception {
    setMinIdle(10);
    setInitWaitTime(new TimeInterval(1L, TimeUnit.SECONDS));
    GenericObjectPool<Worker> pool = (GenericObjectPool<Worker>) createObjectPool();
    try {
        populatePool(pool);
    } finally {
        Closer.closeQuietly(pool);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.junit.Test)

Example 10 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class CommonsPool2TargetSource method createObjectPool.

/**
 * Subclasses can override this if they want to return a specific Commons pool.
 * They should apply any configuration properties to the pool here.
 * <p>Default is a GenericObjectPool instance with the given pool size.
 * @return an empty Commons {@code ObjectPool}.
 * @see GenericObjectPool
 * @see #setMaxSize
 */
protected ObjectPool createObjectPool() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(getMaxSize());
    config.setMaxIdle(getMaxIdle());
    config.setMinIdle(getMinIdle());
    config.setMaxWaitMillis(getMaxWait());
    config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    config.setBlockWhenExhausted(isBlockWhenExhausted());
    return new GenericObjectPool(this, config);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

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