Skip to content

Commit 0feb41a

Browse files
authored
Merge pull request #108 from FOCONIS/always-close-or-rollback
Always close or rollback connection to avoid possible leak
2 parents e8c042e + 5814131 commit 0feb41a

File tree

10 files changed

+34
-0
lines changed

10 files changed

+34
-0
lines changed

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ private void testConnection() {
377377
} finally {
378378
try {
379379
if (conn != null) {
380+
if (!conn.getAutoCommit()) {
381+
conn.rollback();
382+
}
380383
conn.close();
381384
}
382385
} catch (SQLException ex) {

ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionPoolOfflineTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void offline_whenBusy_allowed() throws SQLException, InterruptedException
113113
try (PreparedStatement statement = busy.prepareStatement("select 'hello' from dual")) {
114114
statement.execute();
115115
}
116+
busy.commit();
116117
Thread.sleep(3000);
117118
System.out.println("busy connection closing now");
118119
busy.close();
@@ -155,6 +156,7 @@ public void offlineGetConnection_expect_goesOnline() throws SQLException {
155156
try (Connection connection = pool.getConnection()) {
156157
assertThat(connection).isNotNull();
157158
assertThat(pool.isOnline()).isTrue();
159+
connection.rollback();
158160
}
159161

160162
pool.shutdown();

ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionPoolSpeedTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private ConnectionPool createPool() {
3535
config.setPassword("");
3636
config.setMinConnections(2);
3737
config.setMaxConnections(100);
38+
config.setAutoCommit(true);
3839

3940
return new ConnectionPool("testspeed", config);
4041
}

ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionPoolTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ void getConnection_expect_poolGrowsAboveMin() throws SQLException {
5050
assertThat(pool.status(false).free()).isEqualTo(0);
5151
assertThat(pool.size()).isEqualTo(3);
5252

53+
con2.rollback();
5354
con2.close();
5455
assertThat(pool.status(false).busy()).isEqualTo(2);
5556
assertThat(pool.status(false).free()).isEqualTo(1);
5657
assertThat(pool.size()).isEqualTo(3);
5758

59+
con3.rollback();
5860
con3.close();
5961
assertThat(pool.status(false).busy()).isEqualTo(1);
6062
assertThat(pool.status(false).free()).isEqualTo(2);
6163
assertThat(pool.size()).isEqualTo(3);
6264

65+
con1.rollback();
6366
con1.close();
6467
assertThat(pool.status(false).busy()).isEqualTo(0);
6568
assertThat(pool.status(false).free()).isEqualTo(3);
@@ -72,19 +75,22 @@ void getConnection_explicitUserPassword() throws SQLException {
7275
PreparedStatement statement = connection.prepareStatement("create user testing password '123'");
7376
statement.execute();
7477
statement.close();
78+
connection.rollback();
7579
connection.close();
7680

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

8084
for (int i = 0; i < 10_000; i++) {
8185
Connection another2 = pool.getConnection();
86+
another2.rollback();
8287
another2.close();
8388
}
8489
PoolStatus status0 = pool.status(true);
8590

8691
for (int i = 0; i < 10_000; i++) {
8792
Connection another2 = pool.getConnection();
93+
another2.rollback();
8894
another2.close();
8995
}
9096
PoolStatus status = pool.status(false);
@@ -100,6 +106,7 @@ void unwrapConnection() throws SQLException {
100106
Connection underlying = connection.unwrap(Connection.class);
101107

102108
assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
109+
connection.rollback();
103110
connection.close();
104111
}
105112

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

112119
assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
120+
connection.rollback();
113121
connection.close();
114122
}
115123

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

129+
connection.rollback();
121130
connection.close();
122131
assertThat(connection.isClosed()).isTrue();
123132
}

ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionPoolTrimIdleTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ public void test() throws SQLException, InterruptedException {
4040
Connection con4 = pool.getConnection();
4141
assertThat(pool.size()).isEqualTo(4);
4242

43+
con1.rollback();
4344
con1.close();
45+
con2.rollback();
4446
con2.close();
47+
con3.rollback();
4548
con3.close();
49+
con4.rollback();
4650
con4.close();
4751
assertThat(pool.size()).isEqualTo(4);
4852
assertThat(pool.status(false).free()).isEqualTo(4);
@@ -67,6 +71,7 @@ public void test_withDecreasingActivity_expect_trimToActivityLevel() throws SQLE
6771
con[i] = pool.getConnection();
6872
}
6973
for (int i = 0; i < 10; i++) {
74+
con[i].rollback();
7075
con[i].close();
7176
}
7277

@@ -125,6 +130,7 @@ public void run() {
125130
connection[i] = pool.getConnection();
126131
}
127132
for (int i = 0; i < count; i++) {
133+
connection[i].rollback();
128134
connection[i].close();
129135
}
130136

ebean-datasource/src/test/java/io/ebean/datasource/pool/DataSourcePoolFactoryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void createPool_forLambda() throws Exception {
5454
for (int i = 0; i < 10; i++) {
5555
try (Connection connection = pool.getConnection()) {
5656
connection.hashCode();
57+
connection.rollback();
5758
}
5859
}
5960

ebean-datasource/src/test/java/io/ebean/datasource/pool/ExtendedPreparedStatementTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void extraClose_expect_noObjectAlreadyClosedError() throws SQLException {
3434
// this is an extra call to close()
3535
stmt.close();
3636
}
37+
connection.rollback();
3738
}
3839
}
3940
}

ebean-datasource/src/test/java/io/ebean/datasource/pool/SchemaTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,30 @@ void getConnectionWithSchemaSwitch() throws SQLException {
6767
ResultSet rs = statement.executeQuery("SELECT name FROM test");
6868
assertThat(rs.next()).isTrue();
6969
assertThat(rs.getString("name")).isEqualTo("default schema");
70+
conn.rollback();
7071
}
7172
try (Connection conn = pool.getConnection()) {
7273
conn.setSchema("SCHEMA1");
7374
Statement statement = conn.createStatement();
7475
ResultSet rs = statement.executeQuery("SELECT name FROM test");
7576
assertThat(rs.next()).isTrue();
7677
assertThat(rs.getString("name")).isEqualTo("schema1");
78+
conn.rollback();
7779
}
7880
try (Connection conn = pool.getConnection()) {
7981
conn.setSchema("SCHEMA2");
8082
Statement statement = conn.createStatement();
8183
ResultSet rs = statement.executeQuery("SELECT name FROM test");
8284
assertThat(rs.next()).isTrue();
8385
assertThat(rs.getString("name")).isEqualTo("schema2");
86+
conn.rollback();
8487
}
8588
try (Connection conn = pool.getConnection()) {
8689
Statement statement = conn.createStatement();
8790
ResultSet rs = statement.executeQuery("SELECT name FROM test");
8891
assertThat(rs.next()).isTrue();
8992
assertThat(rs.getString("name")).isEqualTo("default schema");
93+
conn.rollback();
9094
}
9195
}
9296
}

ebean-datasource/src/test/java/io/ebean/datasource/test/FactoryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ void testPreparedStatement() throws Exception {
208208
} catch (SQLException e) {
209209
// expected: Parameter "#1" not set
210210
}
211+
connection.rollback();
211212
}
212213
}
213214
}

ebean-datasource/src/test/java/io/ebean/datasource/test/PostgresInitTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ void test_password2() throws SQLException {
139139
testConnectionWithSelect(newConnection0, "select acol from app.my_table2");
140140
try (Connection newConnection1 = pool.getConnection()) {
141141
testConnectionWithSelect(newConnection1, "select acol from app.my_table2");
142+
newConnection1.rollback();
142143
}
144+
newConnection0.rollback();
143145
}
146+
connection1.rollback();
144147
}
145148

146149
// a new pool switches immediately
@@ -151,8 +154,11 @@ void test_password2() throws SQLException {
151154
testConnectionWithSelect(connP2_1, "select acol from app.my_table2");
152155
try (var connP2_2 = pool2.getConnection()) {
153156
testConnectionWithSelect(connP2_2, "select acol from app.my_table2");
157+
connP2_2.rollback();
154158
}
159+
connP2_1.rollback();
155160
}
161+
connP2_0.rollback();
156162
}
157163

158164
// reset the password back for other tests

0 commit comments

Comments
 (0)