From 2d287fcf6959936ea26ccbf8ecea2cf6073a221c Mon Sep 17 00:00:00 2001 From: Ramasai Tadepalli Date: Mon, 1 Jan 2024 15:05:44 -0500 Subject: [PATCH] Fix connection leak in `JdbcDataSourceDatabase` constructor and pass in username/pass while getting connection --- .../driver/jdbc/JdbcDataSourceDatabase.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/java/driver/jdbc/src/main/java/org/apache/arrow/adbc/driver/jdbc/JdbcDataSourceDatabase.java b/java/driver/jdbc/src/main/java/org/apache/arrow/adbc/driver/jdbc/JdbcDataSourceDatabase.java index 4059759b71..6348e05c9c 100644 --- a/java/driver/jdbc/src/main/java/org/apache/arrow/adbc/driver/jdbc/JdbcDataSourceDatabase.java +++ b/java/driver/jdbc/src/main/java/org/apache/arrow/adbc/driver/jdbc/JdbcDataSourceDatabase.java @@ -34,8 +34,8 @@ public final class JdbcDataSourceDatabase implements AdbcDatabase { private final String username; private final String password; private final JdbcQuirks quirks; - private final Connection connection; private final AtomicInteger counter; + private Connection connection; JdbcDataSourceDatabase( BufferAllocator allocator, @@ -49,22 +49,19 @@ public final class JdbcDataSourceDatabase implements AdbcDatabase { this.username = username; this.password = password; this.quirks = Objects.requireNonNull(quirks); - try { - this.connection = dataSource.getConnection(); - } catch (SQLException e) { - throw JdbcDriverUtil.fromSqlException(e); - } + this.connection = null; this.counter = new AtomicInteger(); } @Override public AdbcConnection connect() throws AdbcException { - final Connection connection; try { - if (username != null && password != null) { - connection = dataSource.getConnection(username, password); - } else { - connection = dataSource.getConnection(); + if (connection == null) { + if (username != null && password != null) { + connection = dataSource.getConnection(username, password); + } else { + connection = dataSource.getConnection(); + } } } catch (SQLException e) { throw JdbcDriverUtil.fromSqlException(e); @@ -79,7 +76,10 @@ public AdbcConnection connect() throws AdbcException { @Override public void close() throws Exception { - connection.close(); + if (connection != null) { + connection.close(); + } + connection = null; } @Override