Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ private void testConnection() {
} finally {
try {
if (conn != null) {
if (!conn.getAutoCommit()) {
conn.rollback();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the connection leak with DB2.
When we just close the connection (=return to pool) and there is an open UOW, the pool cannot close the connection on trim and the pool restarts. This connection stays open and also their tcp/ip connection (more detailed explanation in #107)

}
conn.close();
}
} catch (SQLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void offline_whenBusy_allowed() throws SQLException, InterruptedException
try (PreparedStatement statement = busy.prepareStatement("select 'hello' from dual")) {
statement.execute();
}
busy.commit();
Thread.sleep(3000);
System.out.println("busy connection closing now");
busy.close();
Expand Down Expand Up @@ -155,6 +156,7 @@ public void offlineGetConnection_expect_goesOnline() throws SQLException {
try (Connection connection = pool.getConnection()) {
assertThat(connection).isNotNull();
assertThat(pool.isOnline()).isTrue();
connection.rollback();
}

pool.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private ConnectionPool createPool() {
config.setPassword("");
config.setMinConnections(2);
config.setMaxConnections(100);
config.setAutoCommit(true);

return new ConnectionPool("testspeed", config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ void getConnection_expect_poolGrowsAboveMin() throws SQLException {
assertThat(pool.status(false).free()).isEqualTo(0);
assertThat(pool.size()).isEqualTo(3);

con2.rollback();
con2.close();
assertThat(pool.status(false).busy()).isEqualTo(2);
assertThat(pool.status(false).free()).isEqualTo(1);
assertThat(pool.size()).isEqualTo(3);

con3.rollback();
con3.close();
assertThat(pool.status(false).busy()).isEqualTo(1);
assertThat(pool.status(false).free()).isEqualTo(2);
assertThat(pool.size()).isEqualTo(3);

con1.rollback();
con1.close();
assertThat(pool.status(false).busy()).isEqualTo(0);
assertThat(pool.status(false).free()).isEqualTo(3);
Expand All @@ -72,19 +75,22 @@ void getConnection_explicitUserPassword() throws SQLException {
PreparedStatement statement = connection.prepareStatement("create user testing password '123'");
statement.execute();
statement.close();
connection.rollback();
connection.close();

Connection another = pool.getConnection("testing", "123");
another.close();

for (int i = 0; i < 10_000; i++) {
Connection another2 = pool.getConnection();
another2.rollback();
another2.close();
}
PoolStatus status0 = pool.status(true);

for (int i = 0; i < 10_000; i++) {
Connection another2 = pool.getConnection();
another2.rollback();
another2.close();
}
PoolStatus status = pool.status(false);
Expand All @@ -100,6 +106,7 @@ void unwrapConnection() throws SQLException {
Connection underlying = connection.unwrap(Connection.class);

assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
connection.rollback();
connection.close();
}

Expand All @@ -110,6 +117,7 @@ void getDelegate() throws SQLException {
Connection underlying = pc.delegate();

assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
connection.rollback();
connection.close();
}

Expand All @@ -118,6 +126,7 @@ void isClosed_afterClose() throws SQLException {
Connection connection = pool.getConnection();
assertThat(connection.isClosed()).isFalse();

connection.rollback();
connection.close();
assertThat(connection.isClosed()).isTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public void test() throws SQLException, InterruptedException {
Connection con4 = pool.getConnection();
assertThat(pool.size()).isEqualTo(4);

con1.rollback();
con1.close();
con2.rollback();
con2.close();
con3.rollback();
con3.close();
con4.rollback();
con4.close();
assertThat(pool.size()).isEqualTo(4);
assertThat(pool.status(false).free()).isEqualTo(4);
Expand All @@ -67,6 +71,7 @@ public void test_withDecreasingActivity_expect_trimToActivityLevel() throws SQLE
con[i] = pool.getConnection();
}
for (int i = 0; i < 10; i++) {
con[i].rollback();
con[i].close();
}

Expand Down Expand Up @@ -125,6 +130,7 @@ public void run() {
connection[i] = pool.getConnection();
}
for (int i = 0; i < count; i++) {
connection[i].rollback();
connection[i].close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void createPool_forLambda() throws Exception {
for (int i = 0; i < 10; i++) {
try (Connection connection = pool.getConnection()) {
connection.hashCode();
connection.rollback();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void extraClose_expect_noObjectAlreadyClosedError() throws SQLException {
// this is an extra call to close()
stmt.close();
}
connection.rollback();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,30 @@ void getConnectionWithSchemaSwitch() throws SQLException {
ResultSet rs = statement.executeQuery("SELECT name FROM test");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("name")).isEqualTo("default schema");
conn.rollback();
}
try (Connection conn = pool.getConnection()) {
conn.setSchema("SCHEMA1");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT name FROM test");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("name")).isEqualTo("schema1");
conn.rollback();
}
try (Connection conn = pool.getConnection()) {
conn.setSchema("SCHEMA2");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT name FROM test");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("name")).isEqualTo("schema2");
conn.rollback();
}
try (Connection conn = pool.getConnection()) {
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT name FROM test");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("name")).isEqualTo("default schema");
conn.rollback();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ void testPreparedStatement() throws Exception {
} catch (SQLException e) {
// expected: Parameter "#1" not set
}
connection.rollback();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ void test_password2() throws SQLException {
testConnectionWithSelect(newConnection0, "select acol from app.my_table2");
try (Connection newConnection1 = pool.getConnection()) {
testConnectionWithSelect(newConnection1, "select acol from app.my_table2");
newConnection1.rollback();
}
newConnection0.rollback();
}
connection1.rollback();
}

// a new pool switches immediately
Expand All @@ -151,8 +154,11 @@ void test_password2() throws SQLException {
testConnectionWithSelect(connP2_1, "select acol from app.my_table2");
try (var connP2_2 = pool2.getConnection()) {
testConnectionWithSelect(connP2_2, "select acol from app.my_table2");
connP2_2.rollback();
}
connP2_1.rollback();
}
connP2_0.rollback();
}

// reset the password back for other tests
Expand Down
Loading