commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cyrille Roy <>
Subject DBCP: forcing recycling of small portions of active connections
Date Thu, 02 Oct 2008 17:27:25 GMT

I am working with a big oracle cluster, and I would need to recycle 
active connections when returned from the pool (even if none are 
considered idle by the pool and would have otherwise be reallocated to 
another connection request immediately), as to periodically force a 
small portion connections to be recreated. This is to cope with a broken 
load balancing issue on my Oracle cluster (that misassigns all 
connection to a single db instance). It seems the DBCP does not by 
default support this using the standard properties, so I tried the 
following suggestions following the post at


As anybody a better solution for this? Would the code sample below work? 
I.e. would Pool.invalidateObject() indeed close the underlying object, 
and lack of call to Pool.returnObject() would indeed prevent the connect 
from being served back by the pool (and won't create memory leak)?

    	GenericObjectPool connectionPool = new GenericObjectPool(null) {
    		private final Random m_random = new Random(System.currentTimeMillis());
			public synchronized void returnObject(Object connection) throws Exception {
    			int rResult = m_random.nextInt(99);

			//Force 10% of the connection to recyle upon release from the pool
    			boolean shouldDropConnection = (rResult <= 10);

    			if (shouldDropConnection) {
    				//Ask the driver to close the connection
    				//And skip returning the object from the pool
    				System.out.println("initOracleYamPool: Dropping " + System.identityHashCode(connection)
+  connection);
    			} else {
    				System.out.println("initOracleYamPool: Returning to pool  " + System.identityHashCode(connection)
+ connection);
    				//Normally return the object to the pool
    	String url= ...
		String username=...
		String password=...
    	ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username,
    	//ObjectPool pool, 
    	//KeyedObjectPoolFactory stmtPoolFactory, 
    	//String validationQuery, 
    	//boolean defaultReadOnly, 
    	//boolean defaultAutoCommit) 
    	PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
    				null, //stmtPoolFactory
    				"select 1 from dual", //validationQuery
    				false, //defaultReadOnly
    				true); //defaultAutoCommit
    	PoolingDataSource pds= new PoolingDataSource(connectionPool);

I was trying to follow sample configuration of the pool (for max size 
and min) at 
but it seems the connections are dropped from the pool as soon as they 
become idle (whereas I indeed had set the 
"connectionPool.setMaxIdle(-1);" to a negative value to prevent idle 
connection cleanup)

Thanks in advance for your help,

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message