-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle: unconditionally close Connection
Right now, we check if a connection is closed before closing it. Also, if a plugin fails to customize a Handle, the connection can leak However, this isn't good enough: even if the server closed the connection, we might have borrowed this from a pool, which needs to know we've returned it. Connection.close is specified to no-op if called more than once, so it's safe to just do it regardless. Fixes #2446
- Loading branch information
1 parent
299268b
commit 11bdacf
Showing
5 changed files
with
108 additions
and
45 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jdbi.v3.core; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
import org.jdbi.v3.core.spi.JdbiPlugin; | ||
import org.jdbi.v3.core.statement.Cleanable; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class JdbiOpenLeakTest { | ||
|
||
Connection conn = Mockito.mock(Connection.class); | ||
boolean leased = false; | ||
boolean handleCleaned = false; | ||
Jdbi jdbi = Jdbi.create(() -> { | ||
leased = true; | ||
return conn; | ||
}); | ||
|
||
@Test | ||
void cleanupCustomizeThrows() throws Exception { | ||
jdbi.installPlugin(new JdbiPlugin() { | ||
@Override | ||
public Connection customizeConnection(final Connection conn) throws SQLException { | ||
throw new CosmicRayException(); | ||
} | ||
}); | ||
assertThatThrownBy(jdbi::open) | ||
.isInstanceOf(CosmicRayException.class); | ||
assertThat(leased).isTrue(); | ||
Mockito.verify(conn).close(); | ||
} | ||
|
||
@Test | ||
void cleanupIsClosedThrows() throws Exception { | ||
Mockito.when(conn.isClosed()).thenThrow(CosmicRayException.class); | ||
try (Handle h = jdbi.open()) { | ||
h.addCleanable(new Cleanable() { | ||
@Override | ||
public void close() throws SQLException { | ||
handleCleaned = true; | ||
} | ||
}); | ||
} | ||
assertThat(handleCleaned).isTrue(); | ||
assertThat(leased).isTrue(); | ||
Mockito.verify(conn).close(); | ||
} | ||
|
||
class CosmicRayException extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
} | ||
} |