-
Notifications
You must be signed in to change notification settings - Fork 4
Fix leaking connections when it was not possible to close them. #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
175 changes: 175 additions & 0 deletions
175
ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionPoolCloseTest.java
This file contains hidden or 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,175 @@ | ||
| package io.ebean.datasource.pool; | ||
|
|
||
| import ch.qos.logback.classic.Level; | ||
| import ch.qos.logback.classic.spi.ILoggingEvent; | ||
| import ch.qos.logback.core.read.ListAppender; | ||
| import io.ebean.datasource.DataSourceBuilder; | ||
| import io.ebean.datasource.DataSourcePool; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.SQLSyntaxErrorException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** | ||
| * Tests, if connections are clean when closing them. | ||
| * <p> | ||
| * Test must run with "do not use module path" option in IntelliJ. | ||
| * <p> | ||
| * See also https://github.com/ebean-orm/ebean-datasource/issues/116 for more details | ||
| */ | ||
| class ConnectionPoolCloseTest { | ||
|
|
||
| private static ListAppender<ILoggingEvent> logCapture = new ListAppender<>(); | ||
|
|
||
| private static DataSourcePool pool = createPool(false, false); | ||
| private static DataSourcePool poolRo = createPool(false, true); | ||
| private static DataSourcePool poolEnforce = createPool(true, false); | ||
| private static DataSourcePool poolEnforceRo = createPool(true, true); | ||
|
|
||
|
|
||
| @BeforeAll | ||
| static void before() { | ||
| // attach logback appender to capture log messages | ||
| ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger("io.ebean.datasource")).addAppender(logCapture); | ||
| logCapture.start(); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void beforeEach() { | ||
| logCapture.list.clear(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void after() { | ||
| logCapture.stop(); | ||
| pool.shutdown(); | ||
| poolRo.shutdown(); | ||
| poolEnforce.shutdown(); | ||
| poolEnforceRo.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| void testNoCommitOrRollback() throws SQLException { | ||
|
|
||
| doNoCommitOrRollback(pool); | ||
| assertThat(getAndResetLogWarinings()) | ||
| .hasSize(1) | ||
| .first().asString().startsWith("[WARN] Tried to close a dirty connection at"); | ||
|
|
||
| assertThatThrownBy(() -> doNoCommitOrRollback(poolEnforce)).isInstanceOf(AssertionError.class); | ||
|
|
||
| doNoCommitOrRollback(poolRo); | ||
| assertThat(getAndResetLogWarinings()).isEmpty(); | ||
|
|
||
| doNoCommitOrRollback(poolEnforceRo); | ||
| assertThat(getAndResetLogWarinings()).isEmpty(); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void testCommit() throws SQLException { | ||
| doCommit(pool); | ||
| doCommit(poolRo); | ||
| doCommit(poolEnforce); | ||
| doCommit(poolEnforceRo); | ||
| assertThat(getAndResetLogWarinings()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testRollback() throws SQLException { | ||
| doRollback(pool); | ||
| doRollback(poolRo); | ||
| doRollback(poolEnforce); | ||
| doRollback(poolEnforceRo); | ||
| assertThat(getAndResetLogWarinings()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testExecuteValidSql() throws SQLException { | ||
| doExecute(pool, "SELECT 1"); | ||
| doExecute(poolRo, "SELECT 1"); | ||
| doExecute(poolEnforce, "SELECT 1"); | ||
| doExecute(poolEnforceRo, "SELECT 1"); | ||
| assertThat(getAndResetLogWarinings()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testExecuteInvalidSql() throws SQLException { | ||
| assertThatThrownBy(() -> doExecute(pool, "invalid query")) | ||
| .isInstanceOf(SQLSyntaxErrorException.class) | ||
| .hasNoSuppressedExceptions(); | ||
| // (un)fortunately, we will log a missing rollback, when exception in try-with-resourches is thrown. | ||
| assertThat(getAndResetLogWarinings()) | ||
| .hasSize(1) | ||
| .first().asString().startsWith("[WARN] Tried to close a dirty connection at"); | ||
|
|
||
| assertThatThrownBy(() -> doExecute(poolEnforce, "invalid query")) | ||
| .isInstanceOf(SQLSyntaxErrorException.class) | ||
| .hasSuppressedException(new AssertionError("Tried to close a dirty connection. See https://github.com/ebean-orm/ebean-datasource/issues/116 for details.")); | ||
|
|
||
| assertThatThrownBy(() -> doExecute(poolRo, "invalid query")) | ||
| .isInstanceOf(SQLSyntaxErrorException.class) | ||
| .hasNoSuppressedExceptions(); | ||
|
|
||
| assertThatThrownBy(() -> doExecute(poolEnforceRo, "invalid query")) | ||
| .isInstanceOf(SQLSyntaxErrorException.class) | ||
| .hasNoSuppressedExceptions(); | ||
| assertThat(getAndResetLogWarinings()).isEmpty(); | ||
| } | ||
|
|
||
| private static void doNoCommitOrRollback(DataSourcePool pool) throws SQLException { | ||
| try (Connection connection = pool.getConnection()) { | ||
| // we do nothing here. | ||
| } | ||
| } | ||
|
|
||
| private static void doCommit(DataSourcePool pool) throws SQLException { | ||
| try (Connection connection = pool.getConnection()) { | ||
| connection.commit(); | ||
| } | ||
| } | ||
|
|
||
| private static void doRollback(DataSourcePool pool) throws SQLException { | ||
| try (Connection connection = pool.getConnection()) { | ||
| connection.rollback(); | ||
| } | ||
| } | ||
|
|
||
| private static void doExecute(DataSourcePool pool, String query) throws SQLException { | ||
| try (Connection connection = pool.getConnection()) { | ||
| connection.createStatement().execute(query); | ||
| connection.commit(); | ||
| } | ||
| } | ||
|
|
||
| private static List<ILoggingEvent> getAndResetLogWarinings() { | ||
| List<ILoggingEvent> warnings = logCapture.list.stream().filter(e -> e.getLevel().levelInt >= Level.WARN_INT).collect(Collectors.toList()); | ||
| logCapture.list.clear(); | ||
| return warnings; | ||
| } | ||
|
|
||
| private static DataSourcePool createPool(boolean enforceCleanClose, boolean readOnly) { | ||
| DataSourcePool ret = DataSourceBuilder.create() | ||
| .url("jdbc:h2:mem:tests") | ||
| .username("sa") | ||
| .password("") | ||
| .minConnections(2) | ||
| .maxConnections(4) | ||
| .enforceCleanClose(enforceCleanClose) | ||
| .readOnly(readOnly) | ||
| .build(); | ||
| // clear capturer after create | ||
| logCapture.list.clear(); | ||
| return ret; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.