Skip to content

Commit ebd0501

Browse files
author
raju.gupta
committed
Fix resource leak in performQuery by using Consumer pattern
- Changed performQuery method signature from returning ResultSet to accepting Consumer<ResultSet> - Implemented proper resource management with try-with-resources for Connection, Statement, and ResultSet - Updated all test files across database modules to use new lambda-based pattern - Wrapped assertions in assertThatNoException().isThrownBy() blocks for proper exception handling This change ensures JDBC resources are properly closed after query execution, preventing resource leaks. Note: Additional resource leak fixes will follow in subsequent PRs. This PR focuses on the performQuery enhancement to facilitate easier review.
1 parent 2cce248 commit ebd0501

File tree

21 files changed

+744
-251
lines changed

21 files changed

+744
-251
lines changed

modules/clickhouse/src/test/java/org/testcontainers/clickhouse/ClickHouseContainerTest.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.clickhouse.client.api.Client;
44
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
55
import com.clickhouse.client.api.query.QueryResponse;
6+
import org.assertj.core.api.Assertions;
67
import org.junit.jupiter.api.Test;
78
import org.testcontainers.ClickhouseTestImages;
89
import org.testcontainers.db.AbstractContainerDatabaseTest;
910

10-
import java.sql.ResultSet;
1111
import java.sql.SQLException;
1212
import java.util.concurrent.ExecutionException;
1313
import java.util.concurrent.TimeUnit;
@@ -26,10 +26,18 @@ void testSimple() throws SQLException {
2626
) {
2727
clickhouse.start();
2828

29-
ResultSet resultSet = performQuery(clickhouse, "SELECT 1");
30-
31-
int resultSetInt = resultSet.getInt(1);
32-
assertThat(resultSetInt).isEqualTo(1);
29+
performQuery(
30+
clickhouse,
31+
"SELECT 1",
32+
resultSet -> {
33+
Assertions
34+
.assertThatNoException()
35+
.isThrownBy(() -> {
36+
int resultSetInt = resultSet.getInt(1);
37+
assertThat(resultSetInt).isEqualTo(1);
38+
});
39+
}
40+
);
3341
}
3442
}
3543

@@ -45,13 +53,18 @@ void customCredentialsWithUrlParams() throws SQLException {
4553
) {
4654
clickhouse.start();
4755

48-
ResultSet resultSet = performQuery(
56+
performQuery(
4957
clickhouse,
50-
"SELECT value FROM system.settings where name='max_result_rows'"
58+
"SELECT value FROM system.settings where name='max_result_rows'",
59+
resultSet -> {
60+
Assertions
61+
.assertThatNoException()
62+
.isThrownBy(() -> {
63+
int resultSetInt = resultSet.getInt(1);
64+
assertThat(resultSetInt).isEqualTo(5);
65+
});
66+
}
5167
);
52-
53-
int resultSetInt = resultSet.getInt(1);
54-
assertThat(resultSetInt).isEqualTo(5);
5568
}
5669
}
5770

@@ -60,10 +73,18 @@ void testNewAuth() throws SQLException {
6073
try (ClickHouseContainer clickhouse = new ClickHouseContainer(ClickhouseTestImages.CLICKHOUSE_24_12_IMAGE)) {
6174
clickhouse.start();
6275

63-
ResultSet resultSet = performQuery(clickhouse, "SELECT 1");
64-
65-
int resultSetInt = resultSet.getInt(1);
66-
assertThat(resultSetInt).isEqualTo(1);
76+
performQuery(
77+
clickhouse,
78+
"SELECT 1",
79+
resultSet -> {
80+
Assertions
81+
.assertThatNoException()
82+
.isThrownBy(() -> {
83+
int resultSetInt = resultSet.getInt(1);
84+
assertThat(resultSetInt).isEqualTo(1);
85+
});
86+
}
87+
);
6788
}
6889
}
6990

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.testcontainers.junit.clickhouse;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.jupiter.api.Test;
45
import org.testcontainers.ClickhouseTestImages;
56
import org.testcontainers.containers.ClickHouseContainer;
67
import org.testcontainers.db.AbstractContainerDatabaseTest;
78

8-
import java.sql.ResultSet;
99
import java.sql.SQLException;
1010

1111
import static org.assertj.core.api.Assertions.assertThat;
@@ -17,10 +17,18 @@ public void testSimple() throws SQLException {
1717
try (ClickHouseContainer clickhouse = new ClickHouseContainer(ClickhouseTestImages.CLICKHOUSE_IMAGE)) {
1818
clickhouse.start();
1919

20-
ResultSet resultSet = performQuery(clickhouse, "SELECT 1");
21-
22-
int resultSetInt = resultSet.getInt(1);
23-
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
20+
performQuery(
21+
clickhouse,
22+
"SELECT 1",
23+
resultSet -> {
24+
Assertions
25+
.assertThatNoException()
26+
.isThrownBy(() -> {
27+
int resultSetInt = resultSet.getInt(1);
28+
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
29+
});
30+
}
31+
);
2432
}
2533
}
2634
}

modules/cockroachdb/src/test/java/org/testcontainers/cockroachdb/CockroachContainerTest.java

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.testcontainers.cockroachdb;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.jupiter.api.Test;
45
import org.testcontainers.CockroachDBTestImages;
56
import org.testcontainers.db.AbstractContainerDatabaseTest;
67
import org.testcontainers.images.builder.Transferable;
78

8-
import java.sql.ResultSet;
99
import java.sql.SQLException;
1010
import java.util.logging.Level;
1111
import java.util.logging.LogManager;
@@ -27,10 +27,18 @@ void testSimple() throws SQLException {
2727
) {
2828
cockroach.start();
2929

30-
ResultSet resultSet = performQuery(cockroach, "SELECT 1");
31-
32-
int resultSetInt = resultSet.getInt(1);
33-
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
30+
performQuery(
31+
cockroach,
32+
"SELECT 1",
33+
resultSet -> {
34+
Assertions
35+
.assertThatNoException()
36+
.isThrownBy(() -> {
37+
int resultSetInt = resultSet.getInt(1);
38+
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
39+
});
40+
}
41+
);
3442
}
3543
}
3644

@@ -42,10 +50,20 @@ void testExplicitInitScript() throws SQLException {
4250
) { // CockroachDB is expected to be compatible with Postgres
4351
cockroach.start();
4452

45-
ResultSet resultSet = performQuery(cockroach, "SELECT foo FROM bar");
46-
47-
String firstColumnValue = resultSet.getString(1);
48-
assertThat(firstColumnValue).as("Value from init script should equal real value").isEqualTo("hello world");
53+
performQuery(
54+
cockroach,
55+
"SELECT foo FROM bar",
56+
resultSet -> {
57+
Assertions
58+
.assertThatNoException()
59+
.isThrownBy(() -> {
60+
String firstColumnValue = resultSet.getString(1);
61+
assertThat(firstColumnValue)
62+
.as("Value from init script should equal real value")
63+
.isEqualTo("hello world");
64+
});
65+
}
66+
);
4967
}
5068
}
5169

@@ -80,10 +98,18 @@ void testWithUsernamePasswordDatabase() throws SQLException {
8098
) {
8199
cockroach.start();
82100

83-
ResultSet resultSet = performQuery(cockroach, "SELECT 1");
84-
85-
int resultSetInt = resultSet.getInt(1);
86-
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
101+
performQuery(
102+
cockroach,
103+
"SELECT 1",
104+
resultSet -> {
105+
Assertions
106+
.assertThatNoException()
107+
.isThrownBy(() -> {
108+
int resultSetInt = resultSet.getInt(1);
109+
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
110+
});
111+
}
112+
);
87113

88114
String jdbcUrl = cockroach.getJdbcUrl();
89115
assertThat(jdbcUrl).contains("/" + "test_database");
@@ -123,10 +149,20 @@ void testInitializationScript() throws SQLException {
123149
) { // CockroachDB is expected to be compatible with Postgres
124150
cockroach.start();
125151

126-
ResultSet resultSet = performQuery(cockroach, "SELECT foo FROM bar");
127-
128-
String firstColumnValue = resultSet.getString(1);
129-
assertThat(firstColumnValue).as("Value from init script should equal real value").isEqualTo("hello world");
152+
performQuery(
153+
cockroach,
154+
"SELECT foo FROM bar",
155+
resultSet -> {
156+
Assertions
157+
.assertThatNoException()
158+
.isThrownBy(() -> {
159+
String firstColumnValue = resultSet.getString(1);
160+
assertThat(firstColumnValue)
161+
.as("Value from init script should equal real value")
162+
.isEqualTo("hello world");
163+
});
164+
}
165+
);
130166
}
131167
}
132168
}

modules/cratedb/src/test/java/org/testcontainers/junit/cratedb/SimpleCrateDBTest.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.testcontainers.junit.cratedb;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.jupiter.api.Test;
45
import org.testcontainers.CrateDBTestImages;
56
import org.testcontainers.cratedb.CrateDBContainer;
67
import org.testcontainers.db.AbstractContainerDatabaseTest;
78

8-
import java.sql.ResultSet;
99
import java.sql.SQLException;
1010
import java.util.logging.Level;
1111
import java.util.logging.LogManager;
@@ -26,9 +26,18 @@ void testSimple() throws SQLException {
2626
) {
2727
cratedb.start();
2828

29-
ResultSet resultSet = performQuery(cratedb, "SELECT 1");
30-
int resultSetInt = resultSet.getInt(1);
31-
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
29+
performQuery(
30+
cratedb,
31+
"SELECT 1",
32+
resultSet -> {
33+
Assertions
34+
.assertThatNoException()
35+
.isThrownBy(() -> {
36+
int resultSetInt = resultSet.getInt(1);
37+
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
38+
});
39+
}
40+
);
3241
assertHasCorrectExposedAndLivenessCheckPorts(cratedb);
3342
}
3443
}
@@ -41,9 +50,18 @@ void testCommandOverride() throws SQLException {
4150
) {
4251
cratedb.start();
4352

44-
ResultSet resultSet = performQuery(cratedb, "select name from sys.cluster");
45-
String result = resultSet.getString(1);
46-
assertThat(result).as("cluster name should be overridden").isEqualTo("testcontainers");
53+
performQuery(
54+
cratedb,
55+
"select name from sys.cluster",
56+
resultSet -> {
57+
Assertions
58+
.assertThatNoException()
59+
.isThrownBy(() -> {
60+
String result = resultSet.getString(1);
61+
assertThat(result).as("cluster name should be overridden").isEqualTo("testcontainers");
62+
});
63+
}
64+
);
4765
}
4866
}
4967

@@ -55,10 +73,20 @@ void testExplicitInitScript() throws SQLException {
5573
) {
5674
cratedb.start();
5775

58-
ResultSet resultSet = performQuery(cratedb, "SELECT foo FROM bar");
59-
60-
String firstColumnValue = resultSet.getString(1);
61-
assertThat(firstColumnValue).as("Value from init script should equal real value").isEqualTo("hello world");
76+
performQuery(
77+
cratedb,
78+
"SELECT foo FROM bar",
79+
resultSet -> {
80+
Assertions
81+
.assertThatNoException()
82+
.isThrownBy(() -> {
83+
String firstColumnValue = resultSet.getString(1);
84+
assertThat(firstColumnValue)
85+
.as("Value from init script should equal real value")
86+
.isEqualTo("hello world");
87+
});
88+
}
89+
);
6290
}
6391
}
6492

modules/databend/src/test/java/org/testcontainers/databend/DatabendContainerTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.testcontainers.databend;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.jupiter.api.Test;
45
import org.testcontainers.db.AbstractContainerDatabaseTest;
56

6-
import java.sql.ResultSet;
77
import java.sql.SQLException;
88

99
import static org.assertj.core.api.Assertions.assertThat;
@@ -18,10 +18,18 @@ void testSimple() throws SQLException {
1818
) {
1919
databend.start();
2020

21-
ResultSet resultSet = performQuery(databend, "SELECT 1");
22-
23-
int resultSetInt = resultSet.getInt(1);
24-
assertThat(resultSetInt).isEqualTo(1);
21+
performQuery(
22+
databend,
23+
"SELECT 1",
24+
resultSet -> {
25+
Assertions
26+
.assertThatNoException()
27+
.isThrownBy(() -> {
28+
int resultSetInt = resultSet.getInt(1);
29+
assertThat(resultSetInt).isEqualTo(1);
30+
});
31+
}
32+
);
2533
}
2634
}
2735

@@ -35,10 +43,18 @@ void customCredentialsWithUrlParams() throws SQLException {
3543
) {
3644
databend.start();
3745

38-
ResultSet resultSet = performQuery(databend, "SELECT 1;");
39-
40-
int resultSetInt = resultSet.getInt(1);
41-
assertThat(resultSetInt).isEqualTo(1);
46+
performQuery(
47+
databend,
48+
"SELECT 1;",
49+
resultSet -> {
50+
Assertions
51+
.assertThatNoException()
52+
.isThrownBy(() -> {
53+
int resultSetInt = resultSet.getInt(1);
54+
assertThat(resultSetInt).isEqualTo(1);
55+
});
56+
}
57+
);
4258
}
4359
}
4460
}

0 commit comments

Comments
 (0)