You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/07/25 23:27:56 UTC

commons-dbcp git commit: [DBCP-516] Do not double returnObject back to the pool if there is a transaction context with a shared connection. Closes #17.

Repository: commons-dbcp
Updated Branches:
  refs/heads/master c2efa4a80 -> af598460a


[DBCP-516] Do not double returnObject back to the pool if there is a
transaction context with a shared connection. Closes #17.

Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/af598460
Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/af598460
Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/af598460

Branch: refs/heads/master
Commit: af598460ac9990c2a02823b3cfbdaf9f5429ad5e
Parents: c2efa4a
Author: Gary Gregory <gg...@apache.org>
Authored: Wed Jul 25 17:27:54 2018 -0600
Committer: Gary Gregory <gg...@apache.org>
Committed: Wed Jul 25 17:27:54 2018 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |  3 ++
 .../dbcp2/managed/ManagedConnection.java        |  3 +-
 .../managed/TestManagedDataSourceInTx.java      | 36 ++++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/af598460/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4ca3102..422b308 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -70,6 +70,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="update" issue="DBCP-515" due-to="Tom Jenkinson, Gary Gregory">
         Do not try to register synchronization when the transaction is no longer active.
       </action>
+      <action dev="ggregory" type="update" issue="DBCP-516" due-to="Tom Jenkinson, Gary Gregory">
+        Do not double returnObject back to the pool if there is a transaction context with a shared connection.
+      </action>
     </release>
     <release version="2.5.0" date="2018-07-15" description="This is a minor release, including bug fixes and enhancements.">
       <action dev="ggregory" type="update" issue="DBCP-505" due-to="Gary Gregory">

http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/af598460/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
index 1b1f9de..f9e7d1e 100644
--- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
@@ -105,9 +105,10 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio
             // in the transaction, replace our delegate with the enrolled connection
 
             // return current connection to the pool
+            @SuppressWarnings("resource")
             final C connection = getDelegateInternal();
             setDelegate(null);
-            if (connection != null) {
+            if (connection != null && transactionContext.getSharedConnection() != connection) {
                 try {
                     pool.returnObject(connection);
                 } catch (final Exception ignored) {

http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/af598460/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
index 0dbfe76..8090cc6 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.dbcp2.managed;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
@@ -129,6 +130,41 @@ public class TestManagedDataSourceInTx extends TestManagedDataSource {
         transactionManager.commit();
     }
 
+    @Test
+    public void testDoubleReturn() throws Exception {
+        transactionManager.getTransaction().registerSynchronization(new Synchronization() {
+            private ManagedConnection<?> conn;
+
+            @Override
+            public void beforeCompletion() {
+                try {
+                    conn = (ManagedConnection<?>) ds.getConnection();
+                    assertNotNull(conn);
+                } catch (final SQLException e) {
+                    fail("Could not get connection");
+                }
+            }
+
+            @Override
+            public void afterCompletion(final int i) {
+                final int numActive = pool.getNumActive();
+                try {
+                    conn.checkOpen();
+                } catch (final Exception e) {
+                    // Ignore
+                }
+                assertEquals(numActive, pool.getNumActive());
+                try {
+                    conn.close();
+                } catch (final Exception e) {
+                    fail("Should have been able to close the connection");
+                }
+                // TODO Requires DBCP-515 assertTrue(numActive -1 == pool.getNumActive());
+            }
+        });
+        transactionManager.commit();
+    }
+
     @Override
     @Test
     public void testHashCode() throws Exception {