From 8aff7db2e0214db110627dd25a5440dab92eb661 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Thu, 1 Aug 2013 23:28:26 +0100 Subject: [PATCH] HHH-8410 NaturalIdXrefDelegate could miss some cached entries from naturalIdResolutionCacheMap --- .../engine/internal/NaturalIdXrefDelegate.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java index 29dee5d946..906de573de 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java @@ -55,7 +55,7 @@ public class NaturalIdXrefDelegate { private static final Logger LOG = Logger.getLogger( NaturalIdXrefDelegate.class ); private final StatefulPersistenceContext persistenceContext; - private final Map naturalIdResolutionCacheMap = new ConcurrentHashMap(); + private final ConcurrentHashMap naturalIdResolutionCacheMap = new ConcurrentHashMap(); /** * Constructs a NaturalIdXrefDelegate @@ -92,7 +92,10 @@ public class NaturalIdXrefDelegate { NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get( persister ); if ( entityNaturalIdResolutionCache == null ) { entityNaturalIdResolutionCache = new NaturalIdResolutionCache( persister ); - naturalIdResolutionCacheMap.put( persister, entityNaturalIdResolutionCache ); + NaturalIdResolutionCache previousInstance = naturalIdResolutionCacheMap.putIfAbsent( persister, entityNaturalIdResolutionCache ); + if ( previousInstance != null ) { + entityNaturalIdResolutionCache = previousInstance; + } } return entityNaturalIdResolutionCache.cache( pk, naturalIdValues ); } @@ -279,7 +282,10 @@ public class NaturalIdXrefDelegate { if ( entityNaturalIdResolutionCache == null ) { entityNaturalIdResolutionCache = new NaturalIdResolutionCache( persister ); - naturalIdResolutionCacheMap.put( persister, entityNaturalIdResolutionCache ); + NaturalIdResolutionCache existingCache = naturalIdResolutionCacheMap.putIfAbsent( persister, entityNaturalIdResolutionCache ); + if ( existingCache != null ) { + entityNaturalIdResolutionCache = existingCache; + } } entityNaturalIdResolutionCache.pkToNaturalIdMap.put( pk, cachedNaturalId );