diff --git a/modules/r2dbc/src/main/java/org/testcontainers/r2dbc/TestcontainersR2DBCConnectionFactory.java b/modules/r2dbc/src/main/java/org/testcontainers/r2dbc/TestcontainersR2DBCConnectionFactory.java index bada076eec9..6cdc30b8305 100644 --- a/modules/r2dbc/src/main/java/org/testcontainers/r2dbc/TestcontainersR2DBCConnectionFactory.java +++ b/modules/r2dbc/src/main/java/org/testcontainers/r2dbc/TestcontainersR2DBCConnectionFactory.java @@ -31,7 +31,7 @@ class TestcontainersR2DBCConnectionFactory implements ConnectionFactory, Closeab private final R2DBCDatabaseContainerProvider containerProvider; - private CompletableFuture future; + private volatile CompletableFuture future; TestcontainersR2DBCConnectionFactory(ConnectionFactoryOptions options) { this.options = options; @@ -47,10 +47,12 @@ class TestcontainersR2DBCConnectionFactory implements ConnectionFactory, Closeab @Override public Publisher create() { return new ConnectionPublisher(() -> { - if (future == null) { + CompletableFuture futureRef = future; + if (futureRef == null) { synchronized (this) { - if (future == null) { - future = + futureRef = future; + if (futureRef == null) { + futureRef = CompletableFuture.supplyAsync( () -> { R2DBCDatabaseContainer container = containerProvider.createContainer(options); @@ -59,12 +61,11 @@ public Publisher create() { }, EXECUTOR ); + future = futureRef; } } } - return future.thenApply(it -> { - return ConnectionFactories.find(it.configure(options)); - }); + return futureRef.thenApply(it -> ConnectionFactories.find(it.configure(options))); }); }