Skip to content

Commit 470fda4

Browse files
committed
OAK-11429: JUnit tests with an RDB profile will occasionally fail when run against a automatically starting DB2 docker container. 
Hardened container startup.
1 parent 3837aa8 commit 470fda4

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/RdbConnectionUtils.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public class RdbConnectionUtils {
5050
public static final String IMG = SystemPropertySupplier.create("rdb.docker-image", "").get();
5151
public static final Map<String, String> ENV = parseDockerEnv(SystemPropertySupplier.create("rdb.docker-env", "").get());
5252

53-
private static final boolean RDB_AVAILABLE;
53+
private static boolean RDB_AVAILABLE;
5454
private static GenericContainer<?> rdbContainer;
5555

56-
private static int exposedPort = getPortFromJdbcURL(URL);
56+
private static final int exposedPort = getPortFromJdbcURL(URL);
5757

5858
static {
5959
boolean dockerAvailable = false;
@@ -68,8 +68,7 @@ public class RdbConnectionUtils {
6868
} catch (Throwable t) {
6969
LOG.error("not able to pull specified docker image: {}, error: ", IMG, t);
7070
}
71-
RDB_AVAILABLE = dockerAvailable && imageAvailable;
72-
if (RDB_AVAILABLE) {
71+
if (dockerAvailable && imageAvailable) {
7372
rdbContainer = new GenericContainer<>(DockerImageName.parse(IMG))
7473
.withPrivilegedMode(true)
7574
.withEnv(ENV)
@@ -87,27 +86,24 @@ public class RdbConnectionUtils {
8786
LOG.info("DataSource initialized");
8887
for (int k = 0; k < 30 && !containerReady; k++) {
8988
Thread.sleep(10000);
90-
Connection connection = null;
91-
try {
92-
connection = dataSource.getConnection();
93-
containerReady = true;
89+
try (Connection connection = dataSource.getConnection()) {
90+
if (!connection.isClosed()) {
91+
containerReady = true;
92+
}
9493
} catch (SQLException expected) {
9594
LOG.info("Failed to connect to {}, will retry", url);
96-
} finally {
97-
if (connection != null) {
98-
try {
99-
connection.close();
100-
} catch (SQLException expected) {}
101-
}
10295
}
10396
if (containerReady) {
10497
LOG.info("Container ready");
10598
} else {
10699
LOG.error("Failed to connect to {} within timeout", url);
107100
}
108101
}
102+
RDB_AVAILABLE = true;
109103
} catch (Exception e) {
110104
LOG.error("error while starting RDB container, error: ", e);
105+
RDB_AVAILABLE = false;
106+
rdbContainer.close();
111107
}
112108
}
113109
}

0 commit comments

Comments
 (0)