Hibernate3 in conjunction with ehcache-2.8.3, "another unnamed CacheManager already exists in the same VM" issue in case of multiple sessionfactory instances

Source: Internet
Author: User

Do not want to see the analysis process, pull directly to the bottom of the 4th

1. Issue background

For some reason, hibernate3 and ehcache-2.8.3 are used together in the project, and for some reason, multiple sessionfactory instances are required. In this context, start the project error, the specific exception is as follows:

caused By:net.sf.ehcache.CacheException:Another unnamed CacheManager already exists in the same VM. Please provide unique names forEach cachemanager in the config or DoOne of following:1. Use one of the cachemanager.create ()StaticFactory methods to reuse same CacheManager with same name or create oneifnecessary2. Shutdown the earlier CacheManager before creatingNewOne 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:591) at Net.sf.ehcache.CacheManager.init (Cachemanager.java:385) at Net.sf.ehcache.CacheManager.<init> (cachemanager.java:369) at Org.hibernate.cache.EhCacheProvider.start (Ehcacheprovider.java:124) at Org.hibernate.impl.SessionFactoryImpl.<init> (sessionfactoryimpl.java:183) at Org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1294) at Org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory ( Localsessionfactorybean.java:860)

The idea is that there are two unnamed CacheManager stored in the same VM. Specific code details can be easily found based on the above error stack:

Private voidassertnocachemanagerexistswithsamename (Configuration configuration) {synchronized(CacheManager.class) {            FinalString name; Final Booleanisnamed; if(Configuration.getname ()! =NULL) {Name=Configuration.getname (); Isnamed=true; } Else{Name=Default_name; Isnamed=false; } CacheManager CacheManager=cache_managers_map.get (name); if(CacheManager = =NULL) {cache_managers_map.put (name, This); Cache_managers_reverse_map.put ( This, name); } Else{Configurationsource Configurationsource=cachemanager.getconfiguration (). Getconfigurationsource (); FinalString msg = "another" + (isnamed? ") CacheManager with same name ' "+ name +" ' ":" Unnamed CacheManager ")                        + "already exists in the same VM. Please provide unique names-CacheManager in the config or do one of the following:\n "+" 1.                        Use one of the cachemanager.create () static factory methods to reuse same CacheManager with same name " + "or create one if necessary\n" + "2. Shutdown the earlier CacheManager before creating new one with same name.\n "+" the source of the E Xisting CacheManager is: "+ (Configurationsource = =NULL? "[Programmatically configured]": Configurationsource); Throw Newcacheexception (msg); }        }    }
2. Individually specify the Ehcache profile for each instance of Sessionfactory via Hibernate's configuration item "Hibernate.cache.provider_configuration_file_resource_path"

What can I find in the place where Hibernate and Ehcahe docking?

Observation stack:

At Org.hibernate.cache.EhCacheProvider.start (ehcacheprovider.java:124)
At Org.hibernate.impl.sessionfactoryimpl.<init> (sessionfactoryimpl.java:183)

Send the following code first:

String Configurationresourcename =NULL; if(Properties! =NULL) {Configurationresourcename=(String) properties.get (environment.cache_provider_config); }            if(Stringhelper.isempty (Configurationresourcename)) {Manager=NewCacheManager (); } Else{URL URL=LoadResource (configurationresourcename); Manager=NewCacheManager (URL); }

Here's an important finding:

Start Ehcacheprovider when initializing an sessionfactory instance

Create a CacheManager instance when you start Ehcacheprovider

You can use Hibernate's configuration item Hibernate.cache.provider_configuration_file_resource_path to determine CacheManager configuration when creating an CacheManager instance

Hibernate.cache.provider_configuration_file_resource_path what is the value of this configuration item?

Parse the Org.hibernate.util.ConfigHelper.locateConfig (String) code:

 Public Static Final URL Locateconfig (final  String path) {        try  {            returnNew  URL (path);         Catch (malformedurlexception e) {            return  findasresource (path);        }    }

Not hard to find:

1). Priority is found in the form of a standard URL.

2). After the failure, use the Classpath method to find.

3. How to specify the name of the CacheManager instance in the Ehcache XML configuration file

The Assertnocachemanagerexistswithsamename code in Analysis 1 found that CacheManager's name was obtained from Net.sf.ehcache.config.Configuration.

Net.sf.ehcache.config.Configuration instances are also dependent on the Sax parsing configuration file XML implementation.

Sax parsing needs to be used in conjunction with handler, this is the Net.sf.ehcache.config.BeanHandler class.

After debugging Beanhandler, it is found that the cachemanagername of the configuration instance is the value of the Name property of the Ehcache tag in the ehcache XML configuration file in the config file.

<name= "Jbpmehcachemanager">
4. Summarize the solution:

1). Place multiple Ehcachexxx.xml profiles under Classpath (how many sessionfactory instances to place, file names are different), and the name attribute value of the Ehcache tag in each configuration file is unique.

2). Specify Hibernate.cache.provider_configuration_file_resource_ when each Sessionfactory instance is assembled (if programmatic is specified with the API) The value of path is the corresponding Ehcache configuration name

<propKey= "Hibernate.cache.provider_class">Org.hibernate.cache.EhCacheProvider</prop>                                <!--hibernate3 with ehcache2.5 versions, for multiple sessionfactory instances, you need to specify a separate Ehcache configuration file for each sessionfactory instance -                <!--Hibernate3 The path to this configuration item is as follows: 1. Priority is found in the form of a standard URL. 2. After the failure, use the Classpath method to find. 2 ways are used here.  -                <propKey= "Hibernate.cache.provider_configuration_file_resource_path">Ehcache4common.xml</prop>

--eof--

Hibernate3 in conjunction with ehcache-2.8.3, "another unnamed CacheManager already exists in the same VM" issue in case of multiple sessionfactory instances

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.