Issue Details (XML | Word | Printable)

Key: DBCP-233
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Dain Sundstrom
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Commons Dbcp

Allow connection, statement, and result set to be closed multiple times

Created: 17/Jul/07 04:17 AM   Updated: 16/Mar/09 09:19 PM
Return to search
Component/s: None
Affects Version/s: None
Fix Version/s: 1.3

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works CloseTwice.patch 2007-07-17 04:18 AM Dain Sundstrom 13 kB

Resolution Date: 16/Mar/09 09:19 PM


 Description  « Hide
This patch allows Connection, Statement, PreparedStatement, CallableStatement and ResultSet to be closed multiple times. The first time close is called the resource is closed and any subsequent calls have no effect. This behavior is required as per the JavaDocs for these classes. The patch adds tests for closing all types multiple times and updates any tests that incorrectly assert that a resource can be closed more then once.

This patch fixes DBCP-134 and DBCP-3



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Dain Sundstrom made changes - 17/Jul/07 04:18 AM
Field Original Value New Value
Attachment CloseTwice.patch [ 12361950 ]
Repository Revision Date User Message
ASF #557176 Wed Jul 18 06:46:16 UTC 2007 psteitz Changed behavior to allow Connection, Statement, PreparedStatement,
CallableStatement and ResultSet to be closed multiple times. The first time
close is called the resource is closed and any subsequent calls have no effect.
This behavior is required as per the JavaDocs for these classes. Also added
tests for closing all types multiple times and updated any tests that
incorrectly assert that a resource can not be closed more then once.

JIRA: DBCP-233
Patch provided by Dain Sundstrom
Fixes DBCP-134, DBCP-3
Files Changed
MODIFY /jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/cpdsadapter/ConnectionImpl.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TesterResultSet.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestManual.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestManagedDataSource.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TesterStatement.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDriver.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolablePreparedStatement.java
MODIFY /jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDataSource.java
MODIFY /jakarta/commons/proper/dbcp/trunk/xdocs/changes.xml
MODIFY /jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestConnectionPool.java

Phil Steitz added a comment - 18/Jul/07 06:46 AM
Patch applied. Many thanks.

Phil Steitz made changes - 18/Jul/07 06:46 AM
Status Open [ 1 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Frank Hefter added a comment - 07/Feb/08 09:45 AM
I patched a 1.2.2 version with the above but had ongoing problems with "already closed exceptions".
In addition I would suggest to remove in PoolableConnection line84: the "throw new ..." clause (within the isClosed area).
After this change my application is stable now.

I'm a little concerned about the comment "// XXX should be guarded to happen at most once"
But this is done by the try catch surrounding the invalidateObject(this) and the exception thrown didn't help anyway.

Sorry I had no chance to make a patch file or test this.


Phil Steitz added a comment - 09/Feb/08 04:40 PM
Reopening while we investigate this.

Phil Steitz made changes - 09/Feb/08 04:40 PM
Status Resolved [ 5 ] Reopened [ 4 ]
Resolution Fixed [ 1 ]
Phil Steitz added a comment - 10/Feb/08 07:38 PM
How does you code get connections and where are the exceptions occurring? The changes and tests committed should ensure that connection handles obtained from DBCP datasources can be closed multiple times without exceptions being generated.

Bob Bueckers added a comment - 04/Nov/08 03:37 AM
I'm currently using dbcp 1.2.1 but have no problem updating to a newer release. What do I need to do to get the patch for this issue if I update to the 1.2.2 release? This is a very hot, and time sensitive issue for our project!

Thanks in advance!

Bob


Kavin Du added a comment - 31/Dec/08 08:00 PM - edited
One other repro case is if the underlying database connection is closed (listener bounced for example) from the database end, calling Connection.close() will result in:

java.sql.SQLException: Attempted to use Connection after closed() was called.
at org.apache.commons.dbcp.cpdsadapter.ConnectionImpl.assertOpen(ConnectionImpl.java:87)
at org.apache.commons.dbcp.cpdsadapter.ConnectionImpl.close(ConnectionImpl.java:116)

The connection will stay in the the active pool, using up MaxActive. We need to have it drop off from the pool if it's closed. This is important as we want our application to automatically recover from a database failover and still have a accurate connection count.


Mark Thomas added a comment - 16/Mar/09 09:19 PM
The standard pattern of
Connection conn = null;
try { conn = ... } finally {
if (conn!=null) {
try { conn.close(); } catch (SQLException e) { // Ignore or log }
}
}

should address all of the concerns outlined above. Note that you'll need to build DBCP from svn to get the fix that allows multiple calls to close()

As Phil notes, the test cases cover this quite extensively. If you have a scenario/test case that fails with the latest code from svn please re-open and describe your scenario so we can investigate.


Mark Thomas made changes - 16/Mar/09 09:19 PM
Resolution Fixed [ 1 ]
Status Reopened [ 4 ] Resolved [ 5 ]