Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,6 @@ protected void initializeConnection(final Connection conn) throws SQLException {
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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -80,7 +81,14 @@ public synchronized PooledObject<PoolableConnection> makeObject() throws SQLExce
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<>();
Expand Down