From 2b65c73fb715d7f364da4ad4c2720b47d92219a3 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 21 Nov 2019 20:56:48 -0600 Subject: [PATCH] fixed concurrency problem uncovered during JMH-based throughput testing (Act 3) --- .../sqm/internal/ConcreteSqmSelectQueryPlan.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java index 9c3136dafe..61c4485946 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java @@ -181,10 +181,14 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { // The other option would be to leverage `java.util.concurrent.locks.ReadWriteLock` // to protect access. However, synchronized is much simpler here. We will verify // during throughput testing whether this is an issue and consider changes then - if ( cacheableSqmInterpretation == null ) { + + CacheableSqmInterpretation localCopy = cacheableSqmInterpretation; + + if ( localCopy == null ) { synchronized ( this ) { - if ( cacheableSqmInterpretation == null ) { - cacheableSqmInterpretation = buildCacheableSqmInterpretation( + localCopy = cacheableSqmInterpretation; + if ( localCopy == null ) { + cacheableSqmInterpretation = localCopy = buildCacheableSqmInterpretation( sqm, domainParameterXref, executionContext @@ -193,7 +197,7 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { } } - return cacheableSqmInterpretation; + return localCopy; } private static CacheableSqmInterpretation buildCacheableSqmInterpretation(