* cleaned up when a PooledConnection throws a connectionError event.
*/
public void testConnectionErrorCleanup() throws Exception {
// Setup factory
UserPassKey key = new UserPassKey("username", "password");
GenericKeyedObjectPool pool = new GenericKeyedObjectPool(null);
KeyedCPDSConnectionFactory factory =
new KeyedCPDSConnectionFactory(cpds, pool, null, false);
// Checkout a pair of connections
PooledConnection pcon1 =
((PooledConnectionAndInfo) pool.borrowObject(key))
.getPooledConnection();
Connection con1 = pcon1.getConnection();
PooledConnection pcon2 =
((PooledConnectionAndInfo) pool.borrowObject(key))
.getPooledConnection();
assertEquals(2, pool.getNumActive(key));
assertEquals(0, pool.getNumIdle(key));
// Verify listening
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(key));
assertEquals(0, pool.getNumIdle(key));
// Throw another one - we should be on cleanup list, so ignored
pc.throwConnectionError();
assertEquals(1, pool.getNumActive(key));
assertEquals(0, pool.getNumIdle(key));
// Ask for another connection - should trigger makeObject, which causes
// cleanup, removing listeners.
PooledConnection pcon3 =
((PooledConnectionAndInfo) pool.borrowObject(key))
.getPooledConnection();
assertTrue(!pcon3.equals(pcon1)); // better not get baddie back
assertTrue(!pc.getListeners().contains(factory)); // verify cleanup
assertEquals(2, pool.getNumActive(key));
assertEquals(0, pool.getNumIdle(key));
// Return good connections back to pool
pcon2.getConnection().close();
pcon3.getConnection().close();
assertEquals(2, pool.getNumIdle(key));
assertEquals(0, pool.getNumActive(key));
// Verify pc is closed
try {
pc.getConnection();
fail("Expecting SQLException using closed PooledConnection");
} catch (SQLException ex) {
// expected
}
// Back from the dead - ignore the ghost!
con1.close();
assertEquals(2, pool.getNumIdle(key));
assertEquals(0, pool.getNumActive(key));
// Clear pool
factory.getPool().clear();
assertEquals(0, pool.getNumIdle(key));
}