Repository: commons-dbcp
Updated Branches:
  refs/heads/master 6fac1ecfd -> c50607e7c


Fix [Find|Spot]Bugs locking warning

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

Branch: refs/heads/master
Commit: c50607e7c834422c7fd926a345b1badebd1a2675
Parents: 6fac1ec
Author: Mark Thomas <ma...@apache.org>
Authored: Tue Jun 19 10:28:59 2018 +0100
Committer: Mark Thomas <ma...@apache.org>
Committed: Tue Jun 19 10:28:59 2018 +0100

----------------------------------------------------------------------
 .../dbcp2/managed/ManagedConnection.java        | 30 ++++++++++++--------
 1 file changed, 18 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/c50607e7/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 4906c3a..1b1f9de 100644
--- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
@@ -174,21 +174,24 @@ public class ManagedConnection<C extends Connection> 
extends DelegatingConnectio
     @Override
     public void close() throws SQLException {
         if (!isClosedInternal()) {
+            // Don't actually close the connection if in a transaction. The
+            // connection will be closed by the transactionComplete method.
+            //
+            // DBCP-484 we need to make sure setClosedInternal(true) being
+            // invoked if transactionContext is not null as this value will
+            // be modified by the transactionComplete method which could run
+            // in the different thread with the transaction calling back.
+            lock.lock();
             try {
-                // Don't actually close the connection if in a transaction. The
-                // connection will be closed by the transactionComplete method.
-                //
-                // DBCP-484 we need to make sure setClosedInternal(true) being
-                // invoked if transactionContext is not null as this value will
-                // be modified by the transactionComplete method which could 
run
-                // in the different thread with the transaction calling back.
-                lock.lock();
                 if (transactionContext == null || 
transactionContext.isTransactionComplete()) {
                     super.close();
                 }
             } finally {
-                setClosedInternal(true);
-                lock.unlock();
+                try {
+                    setClosedInternal(true);
+                } finally {
+                    lock.unlock();
+                }
             }
         }
     }
@@ -209,8 +212,11 @@ public class ManagedConnection<C extends Connection> 
extends DelegatingConnectio
 
     protected void transactionComplete() {
         lock.lock();
-        transactionContext.completeTransaction();
-        lock.unlock();
+        try {
+            transactionContext.completeTransaction();
+        } finally {
+            lock.unlock();
+        }
 
         // If we were using a shared connection, clear the reference now that
         // the transaction has completed

Reply via email to