From df0aa1eb2e0623bcd1f52ad9deb46b7c16b589b4 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Sat, 29 Feb 2020 22:24:08 -0500 Subject: [PATCH] Fix a subtle double-checked locking issue --- .../persister/collection/AbstractCollectionPersister.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index dedb604846..f42cd9cb1d 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -744,7 +744,7 @@ public abstract class AbstractCollectionPersister } } - private CollectionLoader standardCollectionLoader; + private volatile CollectionLoader standardCollectionLoader; @Override public void initialize(Object key, SharedSessionContractImplementor session) throws HibernateException { @@ -752,10 +752,13 @@ public abstract class AbstractCollectionPersister determineLoaderToUse( key, session ).load( key, session ); } + // lazily initialize instance field via 'double-checked locking' + // see https://en.wikipedia.org/wiki/Double-checked_locking protected CollectionLoader getStandardCollectionLoader() { CollectionLoader localCopy = standardCollectionLoader; if ( localCopy == null ) { synchronized (this) { + localCopy = standardCollectionLoader; if ( localCopy == null ) { localCopy = createCollectionLoader( LoadQueryInfluencers.NONE ); standardCollectionLoader = localCopy;