Skip to content

Commit

Permalink
Fix close behavior of customized Connection
Browse files Browse the repository at this point in the history
Fixes #2676
  • Loading branch information
stevenschlansker committed Jul 5, 2024
1 parent cedfb98 commit a4651ce
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Fix Connection.close() on JdbiPlugin customizeConnection (#2676)

# 3.45.2

- Move oracle12 module back into main build (#2664, thanks @stoyants !)
Expand Down
7 changes: 2 additions & 5 deletions core/src/main/java/org/jdbi/v3/core/Jdbi.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.jdbi.v3.core.internal.OnDemandExtensions;
import org.jdbi.v3.core.internal.exceptions.Unchecked;
import org.jdbi.v3.core.spi.JdbiPlugin;
import org.jdbi.v3.core.statement.Cleanable;
import org.jdbi.v3.core.statement.DefaultStatementBuilder;
import org.jdbi.v3.core.statement.SqlStatements;
import org.jdbi.v3.core.statement.StatementBuilder;
Expand Down Expand Up @@ -368,8 +367,6 @@ public Handle open() {
() -> "Connection factory " + connectionFactory + " returned a null connection");
final long stop = System.nanoTime();

// this looks like a t-w-r but it is not. The connection is only closed in the error case.
final Cleanable connectionCleaner = connectionFactory.getCleanableFor(conn);
try {
for (JdbiPlugin p : plugins) {
conn = p.customizeConnection(conn);
Expand All @@ -378,7 +375,7 @@ public Handle open() {
StatementBuilder cache = statementBuilderFactory.get().createStatementBuilder(conn);

Handle h = Handle.createHandle(this,
connectionCleaner, // don't use conn::close, the cleanup must be done by the connection factory!
connectionFactory.getCleanableFor(conn), // don't use conn::close, the cleanup must be done by the connection factory!
transactionhandler.get(),
cache,
conn);
Expand All @@ -389,7 +386,7 @@ public Handle open() {
LOG.trace("Jdbi [{}] obtain handle [{}] in {}ms", this, h, MILLISECONDS.convert(stop - start, NANOSECONDS));
return h;
} catch (Throwable t) {
connectionCleaner.closeAndSuppress(t);
connectionFactory.getCleanableFor(conn).closeAndSuppress(t);
throw t;
}
} catch (SQLException e) {
Expand Down
65 changes: 65 additions & 0 deletions core/src/test/java/org/jdbi/v3/core/TestCloseConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.core;

import java.sql.Connection;
import java.sql.SQLException;

import org.jdbi.v3.core.spi.JdbiPlugin;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
public class TestCloseConnection {
@Mock Connection outerCxn;
@Mock Connection innerCxn;

@Test
void closeCustomizedConnection() throws Exception {
final Jdbi jdbi = Jdbi.create(() -> outerCxn);
jdbi.installPlugin(new JdbiPlugin() {
@Override
public Connection customizeConnection(final Connection conn) throws SQLException {
assertThat(conn).isSameAs(outerCxn);
return innerCxn;
}
});
jdbi.useHandle(h -> {});
verify(outerCxn, never()).close();
verify(innerCxn).close();
}

@Test
void customizeConnectionThrows() throws Exception {
final Jdbi jdbi = Jdbi.create(() -> outerCxn);
final var t = new IllegalArgumentException();
jdbi.installPlugin(new JdbiPlugin() {
@Override
public Connection customizeConnection(final Connection conn) throws SQLException {
throw t;
}
});
assertThatThrownBy(() -> jdbi.useHandle(h -> {}))
.isSameAs(t);
verify(outerCxn).close();
verify(innerCxn, never()).close();
}
}

0 comments on commit a4651ce

Please sign in to comment.