diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/ConfigSettings.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/ConfigSettings.java index 98cc27a267..9d6e5d2028 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/ConfigSettings.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/ConfigSettings.java @@ -23,4 +23,6 @@ public interface ConfigSettings { * This is the legacy property name. No need to change it to fit under {@link #PROP_PREFIX} */ String EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName"; + + String EHCACHE_CONFIGURATION_CACHE_MANAGER_NAME = "net.sf.ehcache.cacheManagerName"; } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/EhcacheRegionFactory.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/EhcacheRegionFactory.java index 8e21a4a030..9e41f91993 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/EhcacheRegionFactory.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/EhcacheRegionFactory.java @@ -32,6 +32,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor; import org.jboss.logging.Logger; import static org.hibernate.cache.ehcache.ConfigSettings.EHCACHE_CONFIGURATION_RESOURCE_NAME; +import static org.hibernate.cache.ehcache.internal.HibernateEhcacheUtils.setCacheManagerNameIfNeeded; /** * @author Steve Ebersole @@ -145,11 +146,13 @@ public class EhcacheRegionFactory extends RegionFactoryTemplate { } if ( configurationResourceName == null || configurationResourceName.length() == 0 ) { final Configuration configuration = ConfigurationFactory.parseConfiguration(); + setCacheManagerNameIfNeeded( settings, configuration, properties ); return new CacheManager( configuration ); } else { final URL url = loadResource( configurationResourceName, settings ); final Configuration configuration = HibernateEhcacheUtils.loadAndCorrectConfiguration( url ); + setCacheManagerNameIfNeeded( settings, configuration, properties ); return new CacheManager( configuration ); } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/HibernateEhcacheUtils.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/HibernateEhcacheUtils.java index 9bc1e7637c..ab48208ff3 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/HibernateEhcacheUtils.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/HibernateEhcacheUtils.java @@ -7,12 +7,17 @@ package org.hibernate.cache.ehcache.internal; import java.net.URL; +import java.util.Map; +import java.util.UUID; import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.Configuration; import net.sf.ehcache.config.ConfigurationFactory; import net.sf.ehcache.config.NonstopConfiguration; import net.sf.ehcache.config.TimeoutBehaviorConfiguration.TimeoutBehaviorType; +import org.hibernate.boot.spi.SessionFactoryOptions; + +import static org.hibernate.cache.ehcache.ConfigSettings.EHCACHE_CONFIGURATION_CACHE_MANAGER_NAME; /** @@ -62,6 +67,28 @@ public final class HibernateEhcacheUtils { return config; } + static void setCacheManagerNameIfNeeded(SessionFactoryOptions settings, Configuration configuration, Map properties) { + overwriteCacheManagerIfConfigured( configuration, properties ); + if (configuration.getName() == null) { + String sessionFactoryName = settings.getSessionFactoryName(); + if (sessionFactoryName != null) { + configuration.setName( sessionFactoryName ); + } else { + configuration.setName( "Hibernate " + UUID.randomUUID().toString() ); + } + } + } + + static Configuration overwriteCacheManagerIfConfigured(final Configuration configuration, final Map properties) { + if (properties != null) { + final String cacheManagerName = (String) properties.get( EHCACHE_CONFIGURATION_CACHE_MANAGER_NAME ); + if (cacheManagerName != null) { + configuration.setName( cacheManagerName ); + } + } + return configuration; + } + private static void setupHibernateTimeoutBehavior(NonstopConfiguration nonstopConfig) { nonstopConfig.getTimeoutBehavior().setType( TimeoutBehaviorType.EXCEPTION.getTypeName() ); } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/SingletonEhcacheRegionFactory.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/SingletonEhcacheRegionFactory.java index e98b0f617d..40b80cecff 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/SingletonEhcacheRegionFactory.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/SingletonEhcacheRegionFactory.java @@ -13,6 +13,7 @@ import java.util.concurrent.atomic.AtomicInteger; import net.sf.ehcache.CacheManager; +import net.sf.ehcache.config.Configuration; import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.engine.config.spi.ConfigurationService; @@ -20,6 +21,8 @@ import org.hibernate.engine.config.spi.ConfigurationService; import org.jboss.logging.Logger; import static org.hibernate.cache.ehcache.ConfigSettings.EHCACHE_CONFIGURATION_RESOURCE_NAME; +import static org.hibernate.cache.ehcache.internal.HibernateEhcacheUtils.overwriteCacheManagerIfConfigured; +import static org.hibernate.cache.ehcache.internal.HibernateEhcacheUtils.setCacheManagerNameIfNeeded; /** * @author Steve Ebersole @@ -64,7 +67,9 @@ public class SingletonEhcacheRegionFactory extends EhcacheRegionFactory { try { REFERENCE_COUNT.incrementAndGet(); - return CacheManager.create( HibernateEhcacheUtils.loadAndCorrectConfiguration( url ) ); + Configuration config = HibernateEhcacheUtils.loadAndCorrectConfiguration( url ); + setCacheManagerNameIfNeeded( settings, config, properties ); + return CacheManager.create( config ); } catch (RuntimeException e) { REFERENCE_COUNT.decrementAndGet();