HHH-5824 - Poor multithread performance in SessionFactoryImpl.getQueryCache method

This commit is contained in:
Steve Ebersole 2011-01-21 12:10:18 -06:00 committed by JPAV
parent 144dde0080
commit ddbeaf5625
1 changed files with 21 additions and 30 deletions

View File

@ -38,6 +38,8 @@ import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.naming.Reference; import javax.naming.Reference;
import javax.naming.StringRefAddr; import javax.naming.StringRefAddr;
@ -176,8 +178,8 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
private final transient TransactionManager transactionManager; private final transient TransactionManager transactionManager;
private final transient QueryCache queryCache; private final transient QueryCache queryCache;
private final transient UpdateTimestampsCache updateTimestampsCache; private final transient UpdateTimestampsCache updateTimestampsCache;
private final transient Map queryCaches; private final transient Map<String,QueryCache> queryCaches;
private final transient Map allCacheRegions = new HashMap(); private final transient ConcurrentMap<String,Region> allCacheRegions = new ConcurrentHashMap<String, Region>();
private final transient Statistics statistics; private final transient Statistics statistics;
private final transient EventListeners eventListeners; private final transient EventListeners eventListeners;
private final transient CurrentSessionContext currentSessionContext; private final transient CurrentSessionContext currentSessionContext;
@ -391,7 +393,7 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
updateTimestampsCache = new UpdateTimestampsCache(settings, properties); updateTimestampsCache = new UpdateTimestampsCache(settings, properties);
queryCache = settings.getQueryCacheFactory() queryCache = settings.getQueryCacheFactory()
.getQueryCache(null, updateTimestampsCache, settings, properties); .getQueryCache(null, updateTimestampsCache, settings, properties);
queryCaches = new HashMap(); queryCaches = new HashMap<String,QueryCache>();
allCacheRegions.put( updateTimestampsCache.getRegion().getName(), updateTimestampsCache.getRegion() ); allCacheRegions.put( updateTimestampsCache.getRegion().getName(), updateTimestampsCache.getRegion() );
allCacheRegions.put( queryCache.getRegion().getName(), queryCache.getRegion() ); allCacheRegions.put( queryCache.getRegion().getName(), queryCache.getRegion() );
} }
@ -1069,23 +1071,17 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
public void evictQueryRegion(String regionName) { public void evictQueryRegion(String regionName) {
if (regionName == null) throw new NullPointerException( if (regionName == null) throw new NullPointerException(
"Region-name cannot be null (use Cache#evictDefaultQueryRegion to evict the default query cache)"); "Region-name cannot be null (use Cache#evictDefaultQueryRegion to evict the default query cache)");
synchronized (allCacheRegions) { if (settings.isQueryCacheEnabled()) {
if (settings.isQueryCacheEnabled()) { QueryCache namedQueryCache = queryCaches.get(regionName);
QueryCache namedQueryCache = (QueryCache)queryCaches.get(regionName); // TODO : cleanup entries in queryCaches + allCacheRegions ?
// TODO : cleanup entries in queryCaches + allCacheRegions ? if (namedQueryCache != null) namedQueryCache.clear();
if (namedQueryCache != null) namedQueryCache.clear();
}
} }
} }
public void evictQueryRegions() { public void evictQueryRegions() {
synchronized ( allCacheRegions ) { for ( QueryCache queryCache : queryCaches.values() ) {
Iterator regions = queryCaches.values().iterator(); queryCache.clear();
while ( regions.hasNext() ) { // TODO : cleanup entries in queryCaches + allCacheRegions ?
QueryCache cache = ( QueryCache ) regions.next();
cache.clear();
// TODO : cleanup entries in queryCaches + allCacheRegions ?
}
} }
} }
} }
@ -1145,27 +1141,22 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
return null; return null;
} }
synchronized ( allCacheRegions ) { QueryCache currentQueryCache = queryCaches.get( regionName );
QueryCache currentQueryCache = ( QueryCache ) queryCaches.get( regionName ); if ( currentQueryCache == null ) {
if ( currentQueryCache == null ) { currentQueryCache = settings.getQueryCacheFactory().getQueryCache( regionName, updateTimestampsCache, settings, properties );
currentQueryCache = settings.getQueryCacheFactory().getQueryCache( regionName, updateTimestampsCache, settings, properties ); queryCaches.put( regionName, currentQueryCache );
queryCaches.put( regionName, currentQueryCache ); allCacheRegions.put( currentQueryCache.getRegion().getName(), currentQueryCache.getRegion() );
allCacheRegions.put( currentQueryCache.getRegion().getName(), currentQueryCache.getRegion() );
}
return currentQueryCache;
} }
return currentQueryCache;
} }
public Region getSecondLevelCacheRegion(String regionName) { public Region getSecondLevelCacheRegion(String regionName) {
synchronized ( allCacheRegions ) { return allCacheRegions.get( regionName );
return ( Region ) allCacheRegions.get( regionName );
}
} }
public Map getAllSecondLevelCacheRegions() { public Map getAllSecondLevelCacheRegions() {
synchronized ( allCacheRegions ) { return new HashMap( allCacheRegions );
return new HashMap( allCacheRegions );
}
} }
public boolean isClosed() { public boolean isClosed() {