-
Notifications
You must be signed in to change notification settings - Fork 4
Close connections asyncronously #127
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
3 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
98 changes: 98 additions & 0 deletions
98
ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionPoolBlockTest.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,98 @@ | ||
| package io.ebean.datasource.pool; | ||
|
|
||
| import io.ebean.datasource.DataSourceConfig; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.util.Random; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class ConnectionPoolBlockTest { | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger(ConnectionPoolBlockTest.class); | ||
|
|
||
| private final ConnectionPool pool; | ||
|
|
||
| private final Random random = new Random(); | ||
|
|
||
| private int total; | ||
|
|
||
| ConnectionPoolBlockTest() { | ||
| pool = createPool(); | ||
| } | ||
|
|
||
| private ConnectionPool createPool() { | ||
|
|
||
| DataSourceConfig config = new DataSourceConfig(); | ||
| config.setDriver("org.h2.Driver"); | ||
| config.setUrl("jdbc:h2:mem:tests"); | ||
| config.setUsername("sa"); | ||
| config.setPassword(""); | ||
| config.setMinConnections(1); | ||
| config.setMaxConnections(100); | ||
| config.setAutoCommit(false); | ||
| config.setTrimPoolFreqSecs(1); | ||
| config.setMaxInactiveTimeSecs(1); | ||
| config.setHeartbeatFreqSecs(1); | ||
| config.enforceCleanClose(true); | ||
| return new ConnectionPool("testblock", config); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void after() throws InterruptedException { | ||
| pool.shutdown(); | ||
| } | ||
|
|
||
| /** | ||
| * Yes, this code does some strange things to simulate a blocking close. | ||
| * | ||
| * 1. It does not commit/rollback the pooledConnection | ||
| * 2. because of `enforceCleanClose = true`, the pool tries to close the | ||
| * underlying H2 console. | ||
| * 3. another thread holds a synchronized-lock on that H2 connection, | ||
| * so the pool cannot close that connection quickly! | ||
| * | ||
| * This can happen on network outage, because close may send TCP/IP packet | ||
| * which can slow down the pool whenever IO is done during the pool lock. | ||
| */ | ||
| public void blockPool() { | ||
| try (Connection conn = pool.getConnection()) { | ||
| // we close the underlying h2 connection and start a thread that holds | ||
| // synchronized lock on the h2 connection. | ||
| new Thread(() -> { | ||
| try { | ||
| Connection h2Conn = conn.unwrap(org.h2.jdbc.JdbcConnection.class); | ||
| synchronized (h2Conn) { | ||
| Thread.sleep(1000); | ||
| } | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| }).start(); | ||
| Thread.sleep(50); | ||
| } catch (AssertionError e) { | ||
| // expected | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void test() throws Exception { | ||
|
|
||
| new Thread(this::blockPool).start(); | ||
| // wait for warmup | ||
| Thread.sleep(100); | ||
|
|
||
| long start = System.currentTimeMillis(); | ||
| try (Connection conn = pool.getConnection()) { | ||
| conn.rollback(); | ||
| } | ||
| assertThat(System.currentTimeMillis() - start).isLessThan(100); | ||
| } | ||
| } |
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.