commons-user mailing list archives

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

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 
http://commons.markmail.org/search/?q=list%3Aorg.apache.commons.user+dbcp+force+order%3Adate-backward#query:list%3Aorg.apache.commons.user%20dbcp%20force%20order%3Adate-backward+page:2+mid:2ib7bfpofjytpxpl+state:results

below

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
    				invalidateObject(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
    				super.returnObject(connection);
    				
    			}
			}
    		
    	};
    	connectionPool.setMaxActive(85);
		connectionPool.setMaxIdle(-1);		
		
		
		
    	String url= ...
		String username=...
		String password=...
		
		
		
    	ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username,
password);
    	                                                          //PoolableConnectionFactory(ConnectionFactory
connFactory, 
    	//ObjectPool pool, 
    	//KeyedObjectPoolFactory stmtPoolFactory, 
    	//String validationQuery, 
    	//boolean defaultReadOnly, 
    	//boolean defaultAutoCommit) 
    	PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
    				connectionPool,
    				null, //stmtPoolFactory
    				"select 1 from dual", //validationQuery
    				false, //defaultReadOnly
    				true); //defaultAutoCommit
    	
    	connectionPool.setFactory(poolableConnectionFactory);
    	PoolingDataSource pds= new PoolingDataSource(connectionPool);




I was trying to follow sample configuration of the pool (for max size 
and min) at 
http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/package-summary.html 
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,
Cyrille



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message