Bug 53254

Summary: Support for purging connection pool
Product: Tomcat Modules Reporter: Mike Youngstrom <youngm>
Component: jdbc-poolAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   
Bug Depends on:    
Bug Blocks: 53346    

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;
			}
		}
	}