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());
}
}
}
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()) {
}
}
}
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();
}
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);
}
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);
}
Aggregations