Skip to content

Fix broken double-checked locking in the R2DBC connection factory#11913

Open
ivankrn wants to merge 1 commit into
testcontainers:mainfrom
ivankrn:fix-broken-dcl-in-r2dbc-connection-factory
Open

Fix broken double-checked locking in the R2DBC connection factory#11913
ivankrn wants to merge 1 commit into
testcontainers:mainfrom
ivankrn:fix-broken-dcl-in-r2dbc-connection-factory

Conversation

@ivankrn

@ivankrn ivankrn commented Jul 3, 2026

Copy link
Copy Markdown

This PR fixes a thread-safety issue in TestcontainersR2DBCConnectionFactory caused by the incorrectly implemented double-checked locking.

According to Java's memory model, a concurrently used variable without a volatile keyword lacks happens-before semantics, so this can lead to visibility and/or instruction reordering issues, such as seeing stale values or intermittent exceptions. By adding volatile before CompletableFuture<R2DBCDatabaseContainer> future we ensure that it is visible to other threads immediately after its modification.

Additionally, the futureRef trick reduces volatile read overhead as described in Joshua Bloch's "Effective Java" (see this Oracle article for details), thereby increasing overall performance.

Signed-off-by: Ivan Kropotin <102463202+ivankrn@users.noreply.github.com>
@ivankrn ivankrn requested a review from a team as a code owner July 3, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant