We're updating the issue view to help you get more done. 

ehcache region factory should set a name for the CacheManager

Description

Our application uses 2 session factories created from two different org.hibernate.cfg.Configurations (based on the same config file) and after updating from ehcache 2.4 to 2.6 it crashes because ehcache complains about multiple default cache managers

Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:529)
at net.sf.ehcache.CacheManager.init(CacheManager.java:374)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:259)
at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:87)
... 14 more

This also happens no matter whether you have two org.hibernate.cfg.Configuration or just call buildSessionFactory() twice on the same Configuration.
Our application also crashes because it creates its own CacheManager but that is another topic .

I think that the Hibernate cache region factory should make sure that the CacheManager that it creates cannot cause any conflict with any other CacheManagers no matter whether they are created by Hibernate or the applicaton itself.

Fixing this seems to be quite simple

EhCacheRegionFactory.start()

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 if ( configurationResourceName == null || configurationResourceName.length() == 0 ) { Configuration configuration = ConfigurationFactory.parseConfiguration(); // give the CacheManager some unique name. we could also use UUID or whatever configuration.setName(this.toString()); manager = new CacheManager( configuration ); } else { URL url; try { url = new URL( configurationResourceName ); } catch ( MalformedURLException e ) { url = loadResource( configurationResourceName ); } Configuration configuration = HibernateUtil.loadAndCorrectConfiguration( url ); // give the CacheManager some unique name. we could also use UUID or whatever configuration.setName(this.toString()); manager = new CacheManager( configuration ); }

The same change would be required in SingletonEhCacheRegionFactory

Environment

ehcache 2.6.2

Status

Assignee

Alex Snaps

Reporter

Emanuel Kupcik

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

4.1.8

Priority

Major