Author: psteitz Date: Wed Oct 1 00:59:19 2014 New Revision: 1628584 URL: http://svn.apache.org/r1628584 Log: Added invalidateConnection method to BasicDataSource. JIRA: DBCP-426. Patch due to Kasper Sørensen.
Modified: commons/proper/dbcp/trunk/src/changes/changes.xml commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java Modified: commons/proper/dbcp/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/changes/changes.xml?rev=1628584&r1=1628583&r2=1628584&view=diff ============================================================================== --- commons/proper/dbcp/trunk/src/changes/changes.xml (original) +++ commons/proper/dbcp/trunk/src/changes/changes.xml Wed Oct 1 00:59:19 2014 @@ -60,7 +60,7 @@ The <action> type attribute can be add,u --> <body> - <release version="2.0.2" date="TBD" description="This is a bug fix release."> + <release version="2.1" date="TBD" description="This is minor release, including bug fixes and enhancements."> <action issue="DBCP-420" dev="sebb" type="fix"> InstanceKeyDataSource discards native SQLException when given password does not match password used to create the connection. @@ -76,6 +76,9 @@ The <action> type attribute can be add,u Do not ignore the configured custom eviction policy when creating a BasicDataSource. </action> + <action dev="psteitz" type="add" issue="DBCP-426" due-to="Kasper Sørensen"> + Added invalidateConnection method to BasicDataSource. + </action> </release> <release version="2.0.1" date="24 May 2014" description="This is a bug fix release."> <action dev="markt" type="fix"> Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java?rev=1628584&r1=1628583&r2=1628584&view=diff ============================================================================== --- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java (original) +++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java Wed Oct 1 00:59:19 2014 @@ -1865,6 +1865,47 @@ public class BasicDataSource implements throw new SQLFeatureNotSupportedException(); } + /** + * Manually invalidates a connection, effectively requesting the pool to try + * to close it, remove it from the pool and reclaim pool capacity. + * + * @throws IllegalStateException + * if invalidating the connection failed. + * @since 2.1 + */ + public void invalidateConnection(Connection connection) throws IllegalStateException { + if (connection == null) { + return; + } + if (connectionPool == null) { + throw new IllegalStateException("Cannot invalidate connection: ConnectionPool is null."); + } + + final PoolableConnection poolableConnection; + try { + poolableConnection = connection.unwrap(PoolableConnection.class); + if (poolableConnection == null) { + throw new IllegalStateException( + "Cannot invalidate connection: Connection is not a poolable connection."); + } + } catch (SQLException e) { + throw new IllegalStateException("Cannot invalidate connection: Unwrapping poolable connection failed.", e); + } + + // attempt to close the connection for good measure + try { + connection.close(); + } catch (Exception e) { + // ignore any exceptions here + } + + try { + connectionPool.invalidateObject(poolableConnection); + } catch (Exception e) { + throw new IllegalStateException("Invalidating connection threw unexpected exception", e); + } + } + // ------------------------------------------------------ Protected Methods Modified: commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java?rev=1628584&r1=1628583&r2=1628584&view=diff ============================================================================== --- commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java (original) +++ commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java Wed Oct 1 00:59:19 2014 @@ -453,6 +453,20 @@ public class TestBasicDataSource extends // test OK } } + + public void testInvalidateConnection() throws Exception { + ds.setMaxTotal(2); + Connection conn1 = ds.getConnection(); + Connection conn2 = ds.getConnection(); + ds.invalidateConnection(conn1); + assertTrue(conn1.isClosed()); + assertEquals(1, ds.getNumActive()); + assertEquals(0, ds.getNumIdle()); + Connection conn3 = ds.getConnection(); + conn2.close(); + conn3.close(); + } + /** * JIRA DBCP-93: If an SQLException occurs after the GenericObjectPool is * initialized in createDataSource, the evictor task is not cleaned up.