Issue Type: Bug Bug
Affects Versions: 4.1.8
Assignee: Unassigned
Components: caching (L2)
Created: 17/Nov/12 4:27 AM
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()
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
Project: Hibernate ORM
Priority: Major Major
Reporter: Emanuel Kupcik
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira