Bug 53254 - Support for purging connection pool
Support for purging connection pool
Status: RESOLVED FIXED
Product: Tomcat Modules
Classification: Unclassified
Component: jdbc-pool
unspecified
PC All
: P2 normal (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks: 53346
  Show dependency tree
 
Reported: 2012-05-17 17:59 UTC by Mike Youngstrom
Modified: 2012-06-01 16:23 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Youngstrom 2012-05-17 17:59:28 UTC
It is a common function of a connection pool to support "purging" the pool of all current connections and getting all fresh connections.  Oracle UCP and Websphere pools are two such examples.  It would be great if this pool supported such an operation as well.

It would be useful if the operation were exposed through JMX too.
Comment 1 Filip Hanik 2012-05-18 16:37:38 UTC
Fixed in trunk in r1340160
Fixed in 7.0.x in r1340164
Comment 2 Mike Youngstrom 2012-05-18 19:18:05 UTC
Nice!  Thanks Filip!

For laughs here is the workaround implementation I created yesterday. :)

public void purge() throws Exception {
		long oldMaxAge = dataSource.getMaxAge();
		int oldMaxIdle = dataSource.getMaxIdle();
		int oldMaxActive = dataSource.getMaxActive();
		int oldMinEvictTime = dataSource.getMinEvictableIdleTimeMillis();
		PooledConnection connection = dataSource.getPooledConnection();
		IllegalStateException error = null;
		try {
			dataSource.setMaxActive(1);
			dataSource.setMaxIdle(1);
			dataSource.setMaxAge(1);
			dataSource.setMinEvictableIdleTimeMillis(1);
			int sec = 0;
			Thread.sleep(1001);
			while(dataSource.getActive() > 1 && sec < 29) {
				Thread.sleep(1000);
				sec++;
			}
			dataSource.checkIdle();
			if(dataSource.getActive() > 1) {
				error = new IllegalStateException("Closed all but "+(dataSource.getActive()-1)+" connection(s) after 30 sec.  Try again.");
			}
			connection.close();
		} finally {
			dataSource.setMaxAge(oldMaxAge);
			dataSource.setMaxActive(oldMaxActive);
			dataSource.setMaxIdle(oldMaxIdle);
			dataSource.setMinEvictableIdleTimeMillis(oldMinEvictTime);
			if(error != null) {
				throw error;
			}
		}
	}