-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If the c'tor of a handle throws an exception, ensure that the underlying connection is closed as the caller will not have a chance to do so with try-with-resources This fixes the cause of jdbi#2446
- Loading branch information
Showing
5 changed files
with
172 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
core/src/test/java/org/jdbi/v3/core/JdbiOpenLeakTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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 java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.jdbi.v3.core.junit5.H2DatabaseExtension; | ||
import org.jdbi.v3.core.spi.JdbiPlugin; | ||
import org.jdbi.v3.core.transaction.LocalTransactionHandler; | ||
import org.jdbi.v3.core.transaction.TransactionHandler; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JdbiOpenLeakTest { | ||
|
||
@RegisterExtension | ||
public H2DatabaseExtension h2Extension = H2DatabaseExtension.withSomething(); | ||
|
||
@Test | ||
void cleanupCustomizeThrows() throws Exception { | ||
|
||
var jdbiPlugin = new JdbiPlugin() { | ||
@Override | ||
public Connection customizeConnection(final Connection conn) throws SQLException { | ||
throw new CosmicRayException(); | ||
} | ||
}; | ||
|
||
h2Extension.getJdbi().installPlugin(jdbiPlugin); | ||
|
||
h2Extension.clearLastConnection(); | ||
assertThatThrownBy(() -> h2Extension.getJdbi().open()) | ||
.isInstanceOf(CosmicRayException.class); | ||
|
||
assertThat(h2Extension.getLastConnection()).isNotNull(); // has been created | ||
assertThat(h2Extension.getLastConnection().isClosed()).isTrue(); // has been closed | ||
} | ||
|
||
@Test | ||
void cleanupIsClosedThrows() throws Exception { | ||
Connection conn = mock(Connection.class); | ||
AtomicBoolean leased = new AtomicBoolean(false); | ||
AtomicBoolean handleCleaned = new AtomicBoolean(false); | ||
|
||
Jdbi jdbi = Jdbi.create(() -> { | ||
leased.set(true); | ||
return conn; | ||
}); | ||
|
||
when(conn.isClosed()).thenThrow(CosmicRayException.class); | ||
assertThat(leased.get()).isFalse(); | ||
assertThat(handleCleaned.get()).isFalse(); | ||
|
||
try (Handle h = jdbi.open()) { | ||
h.addCleanable(() -> handleCleaned.set(true)); | ||
} | ||
|
||
assertThat(handleCleaned.get()).isTrue(); | ||
assertThat(leased.get()).isTrue(); | ||
verify(conn).close(); | ||
} | ||
|
||
@Test | ||
public void testIssue2446() throws Exception { | ||
|
||
h2Extension.getSharedHandle().execute("insert into something (id, name) values (1, 'Brian')"); | ||
|
||
Jdbi jdbi = h2Extension.getJdbi(); | ||
final ExplodeInSpecializeTransactionHandler handler = new ExplodeInSpecializeTransactionHandler(); | ||
h2Extension.getJdbi().setTransactionHandler(handler); | ||
|
||
assertThatThrownBy(() -> { | ||
String value; | ||
h2Extension.clearLastConnection(); // reset connection | ||
|
||
try (Handle handle = jdbi.open()) { | ||
value = handle.createQuery("select name from something where id = 1").mapToBean(Something.class).one().getName(); | ||
} | ||
assertThat(value).isEqualTo("Brian"); | ||
}).isInstanceOf(ConnectionException.class) | ||
.hasMessageContaining("transaction specialization failure!") | ||
.hasCauseInstanceOf(SQLException.class); | ||
|
||
// see if the c'tor leaked a connection | ||
assertThat(h2Extension.getLastConnection()).isNotNull(); // connection has been checked out | ||
assertThat(h2Extension.getLastConnection().isClosed()).isTrue(); // connection has been closed | ||
} | ||
|
||
static class CosmicRayException extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
} | ||
|
||
static class ExplodeInSpecializeTransactionHandler extends LocalTransactionHandler { | ||
@Override | ||
public TransactionHandler specialize(Handle handle) throws SQLException { | ||
throw new SQLException("transaction specialization failure!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters