From 31ab12f86fdef1222d23040c58d3761cf64d3621 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Sat, 19 Nov 2011 23:58:14 +0800 Subject: [PATCH] HHH-5945 Race condition in building query cache --- .../internal/SessionFactoryImpl.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 5c44fce4bc..36de5186bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -194,7 +194,7 @@ public final class SessionFactoryImpl private transient SchemaExport schemaExport; private final transient QueryCache queryCache; private final transient UpdateTimestampsCache updateTimestampsCache; - private final transient Map queryCaches; + private final transient ConcurrentMap queryCaches; private final transient ConcurrentMap allCacheRegions = new ConcurrentHashMap(); private final transient CurrentSessionContext currentSessionContext; private final transient SQLFunctionRegistry sqlFunctionRegistry; @@ -463,7 +463,7 @@ public final class SessionFactoryImpl updateTimestampsCache = new UpdateTimestampsCache(settings, properties, this); queryCache = settings.getQueryCacheFactory() .getQueryCache(null, updateTimestampsCache, settings, properties); - queryCaches = new HashMap(); + queryCaches = new ConcurrentHashMap(); allCacheRegions.put( updateTimestampsCache.getRegion().getName(), updateTimestampsCache.getRegion() ); allCacheRegions.put( queryCache.getRegion().getName(), queryCache.getRegion() ); } @@ -793,7 +793,7 @@ public final class SessionFactoryImpl updateTimestampsCache = new UpdateTimestampsCache( settings, properties, this ); queryCache = settings.getQueryCacheFactory() .getQueryCache( null, updateTimestampsCache, settings, properties ); - queryCaches = new HashMap(); + queryCaches = new ConcurrentHashMap(); allCacheRegions.put( updateTimestampsCache.getRegion().getName(), updateTimestampsCache.getRegion() ); allCacheRegions.put( queryCache.getRegion().getName(), queryCache.getRegion() ); } @@ -1416,7 +1416,7 @@ public final class SessionFactoryImpl } public boolean containsQuery(String regionName) { - return queryCaches.get( regionName ) != null; + return queryCaches.containsKey( regionName ); } public void evictDefaultQueryRegion() { @@ -1502,11 +1502,18 @@ public final class SessionFactoryImpl QueryCache currentQueryCache = queryCaches.get( regionName ); if ( currentQueryCache == null ) { - currentQueryCache = settings.getQueryCacheFactory().getQueryCache( regionName, updateTimestampsCache, settings, properties ); - queryCaches.put( regionName, currentQueryCache ); - allCacheRegions.put( currentQueryCache.getRegion().getName(), currentQueryCache.getRegion() ); + synchronized ( allCacheRegions ) { + currentQueryCache = queryCaches.get( regionName ); + if ( currentQueryCache == null ) { + currentQueryCache = settings.getQueryCacheFactory() + .getQueryCache( regionName, updateTimestampsCache, settings, properties ); + queryCaches.put( regionName, currentQueryCache ); + allCacheRegions.put( currentQueryCache.getRegion().getName(), currentQueryCache.getRegion() ); + } else { + return currentQueryCache; + } + } } - return currentQueryCache; }