Skip to content

Commit 9660137

Browse files
authored
Fix connection leak in JDBC waitUntilContainerStarted (#5281)
Only the Statement was auto-closed, but not the Connection from which the Statement was created. Aside from resources leaking, this can cause problems when database rename transactions are executed by clients.
1 parent 600d235 commit 9660137

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.testcontainers.containers;
22

33
import com.github.dockerjava.api.command.InspectContainerResponse;
4-
import java.sql.Statement;
54
import lombok.NonNull;
65
import lombok.SneakyThrows;
76
import org.apache.commons.lang3.StringUtils;
@@ -16,6 +15,7 @@
1615
import java.sql.Connection;
1716
import java.sql.Driver;
1817
import java.sql.SQLException;
18+
import java.sql.Statement;
1919
import java.util.HashMap;
2020
import java.util.Map;
2121
import java.util.Properties;
@@ -144,7 +144,8 @@ protected void waitUntilContainerStarted() {
144144
if (!isRunning()) {
145145
Thread.sleep(100L);
146146
} else {
147-
try (Statement statement = createConnection("").createStatement()) {
147+
try (Connection connection = createConnection("");
148+
Statement statement = connection.createStatement()) {
148149
boolean testQuerySucceeded = statement.execute(this.getTestQueryString());
149150
if (testQuerySucceeded) {
150151
logger().info("Container is started (JDBC URL: {})", this.getJdbcUrl());

0 commit comments

Comments
 (0)