Repository: commons-dbcp Updated Branches: refs/heads/master baa2adbd3 -> 131e4926b
[DBCP-484] Connection leak during XATransaction in high load. Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/131e4926 Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/131e4926 Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/131e4926 Branch: refs/heads/master Commit: 131e4926b7a31fff3c6bead10bbe6dcd15e32bf9 Parents: baa2adb Author: Emanuel Freitas <[email protected]> Authored: Mon May 21 08:43:53 2018 -0600 Committer: Gary Gregory <[email protected]> Committed: Mon May 21 08:43:53 2018 -0600 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +++ .../commons/dbcp2/managed/ManagedConnection.java | 13 +++++++++++++ 2 files changed, 16 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/131e4926/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d329d67..8ce59f5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -61,6 +61,9 @@ The <action> type attribute can be add,update,fix,remove. <body> <release version="2.3.1" date="2018-MM-DD" description="This is a minor release, including bug fixes and enhancements."> + <action dev="ggregory" type="fix" issue="DBCP-484" due-to="Emanuel Freitas"> + Connection leak during XATransaction in high load. + </action> </release> <release version="2.3.0" date="2018-05-12" description="This is a minor release, including bug fixes and enhancements."> <action dev="pschumacher" type="fix" issue="DBCP-476" due-to="Gary Evesson, Richard Cordova"> http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/131e4926/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 a66a7a8..c50327f 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java @@ -22,6 +22,8 @@ import org.apache.commons.pool2.ObjectPool; import java.sql.Connection; import java.sql.SQLException; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * ManagedConnection is responsible for managing a database connection in a transactional environment @@ -48,6 +50,7 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio private final boolean accessToUnderlyingConnectionAllowed; private TransactionContext transactionContext; private boolean isSharedConnection; + private Lock lock; public ManagedConnection(final ObjectPool<C> pool, final TransactionRegistry transactionRegistry, @@ -56,6 +59,7 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio this.pool = pool; this.transactionRegistry = transactionRegistry; this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed; + this.lock = new ReentrantLock(); updateTransactionStatus(); } @@ -162,11 +166,18 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio 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) { super.close(); } } finally { setClosedInternal(true); + lock.unlock(); } } } @@ -186,7 +197,9 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio } protected void transactionComplete() { + lock.lock(); transactionContext = null; + lock.unlock(); // If we were using a shared connection, clear the reference now that // the transaction has completed
