Skip to content

Commit

Permalink
HHH-7095 Handle UnsupportedOperationException from RegionFactory.buil…
Browse files Browse the repository at this point in the history
…dNaturalIdRegion

Disable 2L naturalId caching if an UnsupportedOperationException is thrown
  • Loading branch information
edalquist committed Feb 21, 2012
1 parent 13f4c83 commit e18cba8
Showing 1 changed file with 22 additions and 8 deletions.
Expand Up @@ -68,6 +68,7 @@
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.QueryCache;
import org.hibernate.cache.spi.Region;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.UpdateTimestampsCache;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
Expand Down Expand Up @@ -276,7 +277,8 @@ public void handleEntityNotFound(String entityName, Serializable id) {
LOG.debugf( "Instantiating session factory with properties: %s", properties );

// Caches
settings.getRegionFactory().start( settings, properties );
final RegionFactory regionFactory = settings.getRegionFactory();
regionFactory.start( settings, properties );
this.queryPlanCache = new QueryPlanCache( this );

// todo : everything above here consider implementing as standard SF service. specifically: stats, caches, types, function-reg
Expand Down Expand Up @@ -344,7 +346,7 @@ public void sessionFactoryClosed(SessionFactory factory) {
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Building cache for entity data [{0}]", model.getEntityName() );
}
EntityRegion entityRegion = settings.getRegionFactory().buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
EntityRegion entityRegion = regionFactory.buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
accessStrategy = entityRegion.buildAccessStrategy( accessType );
entityAccessStrategies.put( cacheRegionName, accessStrategy );
allCacheRegions.put( cacheRegionName, entityRegion );
Expand All @@ -357,10 +359,22 @@ public void sessionFactoryClosed(SessionFactory factory) {
naturalIdAccessStrategy = ( NaturalIdRegionAccessStrategy ) entityAccessStrategies.get( naturalIdCacheRegionName );

if ( naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
final NaturalIdRegion naturalIdRegion = settings.getRegionFactory().buildNaturalIdRegion( naturalIdCacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( settings.getRegionFactory().getDefaultAccessType() );
entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
allCacheRegions.put( naturalIdCacheRegionName, naturalIdRegion );
final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode( model );

NaturalIdRegion naturalIdRegion = null;
try {
naturalIdRegion = regionFactory.buildNaturalIdRegion( naturalIdCacheRegionName, properties,
cacheDataDescription );
}
catch ( UnsupportedOperationException e ) {
LOG.warn( regionFactory.getClass().getName() + " threw an UnsupportedOperationException for buildNaturalIdRegion, second-level NaturalId caching will not be enabled for " + model.getEntityName() );
}

if (naturalIdRegion != null) {
naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( regionFactory.getDefaultAccessType() );
entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
allCacheRegions.put( naturalIdCacheRegionName, naturalIdRegion );
}
}
}

Expand Down Expand Up @@ -389,7 +403,7 @@ public void sessionFactoryClosed(SessionFactory factory) {
if ( LOG.isTraceEnabled() ) {
LOG.tracev("Building cache for collection data [{0}]", model.getRole() );
}
CollectionRegion collectionRegion = settings.getRegionFactory().buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
CollectionRegion collectionRegion = regionFactory.buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
.decode( model ) );
accessStrategy = collectionRegion.buildAccessStrategy( accessType );
entityAccessStrategies.put( cacheRegionName, accessStrategy );
Expand Down Expand Up @@ -1495,7 +1509,7 @@ public void evictNaturalIdRegion(String entityName) {
EntityPersister p = getEntityPersister( entityName );
if ( p.hasNaturalIdCache() ) {
if ( LOG.isDebugEnabled() ) {
LOG.debugf( "Evicting second-level cache: %s", p.getEntityName() );
LOG.debugf( "Evicting natural-id cache: %s", p.getEntityName() );
}
p.getNaturalIdCacheAccessStrategy().evictAll();
}
Expand Down

0 comments on commit e18cba8

Please sign in to comment.