HHH-7809 override or set cachemanager name

This commit is contained in:
Alex Snaps 2018-03-28 08:12:18 -04:00 committed by Steve Ebersole
parent 01ea47bd92
commit 97dd42cfa6
4 changed files with 38 additions and 1 deletions

View File

@ -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";
}

View File

@ -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 );
}
}

View File

@ -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() );
}

View File

@ -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();