Skip to content

Commit

Permalink
fix(java/driver/jdbc): fix connection leak in `JdbcDataSourceDatabase…
Browse files Browse the repository at this point in the history
…` constructor (#1418)

I think there is a connection leak in the constructor for
`JdbcDataSourceDatabase` where we're obtaining a connection in the
constructor but we're obtaining another one in the `connect` method
using the appropriate username/password. We can remove this and fix the
bug as described in #1415.

Fixes #1415.
  • Loading branch information
rtadepalli authored Jan 3, 2024
1 parent 76ab8cb commit 61c9236
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 61c9236

Please sign in to comment.