Skip to content

Commit 914130f

Browse files
committed
Fix potential resource leak on exception in initializeConnection()
1 parent 8fda43f commit 914130f

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ protected void initializeConnection(final Connection conn) throws SQLException {
425425
for (final String sql : sqls) {
426426
statement.execute(Objects.requireNonNull(sql, "null connectionInitSqls element"));
427427
}
428+
} catch (SQLException sqle) {
429+
/*
430+
* Need to close the connection here as the reference to it will be lost once the SQLEXception is
431+
* thrown.
432+
*
433+
* Cast to AutoCloseable to avoid calling the deprecated method. The cast can be removed once the
434+
* deprecated method has been removed.s
435+
*/
436+
Utils.closeQuietly((AutoCloseable) conn);
437+
throw sqle;
428438
}
429439
}
430440
}

0 commit comments

Comments
 (0)