This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push:
new f8984f38 Handle SQLException on connection initialization with SQL
statements (#540)
f8984f38 is described below
commit f8984f38d9fa562167f4a7582ab5fe58a92df4c9
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Dec 16 19:04:18 2025 -0500
Handle SQLException on connection initialization with SQL statements (#540)
* Revert "Fix potential resource leak on exception in
initializeConnection()"
This reverts commit 914130fbb13dd86a117096f6a725092390dcbd02.
* Handle SQLException from
PoolableConnectionFactory.initializeConnection()
- Revert previous commit
- The call site that creates the connection closes it, just like
PoolableConnectionFactory.makeObject()
---
.../org/apache/commons/dbcp2/PoolableConnectionFactory.java | 10 ----------
.../dbcp2/managed/PoolableManagedConnectionFactory.java | 10 +++++++++-
2 files changed, 9 insertions(+), 11 deletions(-)
diff --git
a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
index a2ab9772..bdcdddc6 100644
--- a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
@@ -425,16 +425,6 @@ public class PoolableConnectionFactory implements
PooledObjectFactory<PoolableCo
for (final String sql : sqls) {
statement.execute(Objects.requireNonNull(sql, "null
connectionInitSqls element"));
}
- } catch (SQLException sqle) {
- /*
- * Need to close the connection here as the reference to it
will be lost once the SQLEXception is
- * thrown.
- *
- * Cast to AutoCloseable to avoid calling the deprecated
method. The cast can be removed once the
- * deprecated method has been removed.s
- */
- Utils.closeQuietly((AutoCloseable) conn);
- throw sqle;
}
}
}
diff --git
a/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
b/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
index 96cd4769..c9142524 100644
---
a/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
+++
b/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
@@ -28,6 +28,7 @@ import org.apache.commons.dbcp2.PStmtKey;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingConnection;
+import org.apache.commons.dbcp2.Utils;
import org.apache.commons.pool2.KeyedObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
@@ -80,7 +81,14 @@ public class PoolableManagedConnectionFactory extends
PoolableConnectionFactory
if (conn == null) {
throw new IllegalStateException("Connection factory returned null
from createConnection");
}
- initializeConnection(conn);
+ try {
+ initializeConnection(conn);
+ } catch (final SQLException e) {
+ // Make sure the connection is closed
+ Utils.closeQuietly((AutoCloseable) conn);
+ // Rethrow original exception so it is visible to caller
+ throw e;
+ }
if (getPoolStatements()) {
conn = new PoolingConnection(conn);
final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement>
config = new GenericKeyedObjectPoolConfig<>();