Ehcache configuration for multiple hibernate.cfg.xml

2,238 views
Skip to first unread message

Niks

unread,
Mar 22, 2016, 1:22:15 PM3/22/16
to ehcache-users
Hi,

I have a set up in my application wherein we have multiple hibernate.cfg.xml to connect to different databases in same sql server

DatabaseA-hibernate.cfg.xml
DatabaseB-hibernate.cfg.xml
DatabaseC-hibernate.cfg.xml

But just one ehcache.xml with default configuration. 

1) Can someone guide me on how to configure second level cache using ehcache , for such scenario. I am getting following warning and not sure if it is because I have not configured separate ehcache for different databases.

WARN net.sf.ehcache.CacheManager:611 - Creating a new instance of CacheManager using the diskStorePath ".......AppData\Local\Temp\" which is already used by an existing CacheManager.
The source of the configuration was net.sf.ehcache.config.generator.ConfigurationSource$DefaultConfigurationSource@2fc07.
The diskStore path for this CacheManager will be set to .....AppData\Local\Temp\\ehcache_auto_created_xyz.

2) Read some of the forums to replace org.hibernate.cache.ehcache.EhCacheRegionFactory with org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory for hibernate.cache.region.factory_class to get rid of above warning

But really want to understand what is the correct configuration. Please advise.

Current configuration:



1) Following jars are used.

hibernate-ehcache-4.3.7.Final.jar
ehcache-core-2.4.3.jar

Hibernate version 4.3.7

2) Not creating CacheManager or adding programatically anything to it. Using all defaults.

Following is the configuration in ehcache.xml

<defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="300"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="300"
            memoryStoreEvictionPolicy="LRU"
            />
<!-- The unnamed query cache -->
   <cache
    name="org.hibernate.cache.StandardQueryCache"
    maxElementsInMemory="1000"
    eternal="false"
    timeToLiveSeconds="300"
    overflowToDisk="false"
   />

3) Properties in hibernate.cfg.xml for 2nd level cache


<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>

4) JDK version 1.8


Niks

Fabien Sanglier

unread,
Mar 24, 2016, 2:56:42 PM3/24/16
to ehcach...@googlegroups.com
Hey,

So let's start from the beginning: 

1) the factories:

"org.hibernate.cache.ehcache.EhCacheRegionFactory", hibernate creates a new cache manager per new hibernate context.

You can check the code of that factory below, and see that indeed it's calling new CacheManager() each time it's started...

In the case of org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory, it's using "manager = CacheManager.create();" which is a singleton call for cachemanager, which means that for any number of hibernate context being started, only 1 single cachemanager will be created and used.


2) the disk space
Each cache manager needs it's own disk space to perform "overflow to disk" functions without stepping on each other...If you have multiple cachemanagers created out of the same ehcache.xml (which has a diskstore path set by default if not specified), you'll get that WARN line below because multiple cachemanager will end up having the same diskstore path which is not allowed.
And what's nice is that ehcache detects that, and creates a different auto-generated disk path for you when that happens.


So overall, with all that in mind, it's easy to understand why you're getting these warnings when using EhCacheRegionFactory (multiple cache managers with same diskstore path) vs. SingletonEhCacheRegionFactory (only 1 single cachemanager in your app = no diskstore path issue)


As a final point, there are no absolute answer as to which configuration is better...
If you want to really keep things separate between your hibernate configs, cache included, I'd go the EhCacheRegionFactory route, and accept that the WARN is just a warning that does not hurt anything.

If you don't mind having all your multiple databases cached in a single cache manager, the SingletonEhCacheRegionFactory makes things easier. 

One thing  I'd be careful in your case (of multiple hibernate contexts) with SingletonEhCacheRegionFactory is possible cachename collisions between the multiple hibernate context if you were to use same cache names acrross your hibernate context (but that's pretty unlikely)

Hope that helps overall.

Fabien

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/bbd51c60-034a-448d-8ce0-e3c1143895fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Fabien Sanglier
fabiens...@gmail.com

Niks

unread,
Mar 29, 2016, 6:37:37 AM3/29/16
to ehcache-users
Thanks Fabien. Really helpful.

Some updates and some more questions if you could please reply.

I want to go with EhCacheRegionFactory, which creates different cacheManager for different hibernate instances. 

I upgraded my ehcache to 2.8.3 from default 2.4.3 that comes with hibernate 4.3.7.Final, which does not allow same name for multiple cacheManagers. 
I also created separate ehcache.xml for each hibernate.cfg.xml by adding ResourceName

<property name="net.sf.ehcache.configurationResourceName">DatabaseA-ehcache.xml</property>

In DatabaseA-ehcache.xml ,I added below to specify the CacheManager name


         xsi:noNamespaceSchemaLocation="ehcache.xsd"
             updateCheck="true" monitoring="autodetect" dynamicConfig="true" name="DatabaseACacheManager" >


1) I get the error: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]

Caused by: org.hibernate.cache.CacheException: 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 ]

Why is ehcache still not able to create separate cacheManager.

I want hibernate to create CacheManager, not doing programatically.

2) Above exception is while creating sessionFactory in my program 

SessionFactory sessionFactory = configuration.buildSessionFactory(registryBuilder.build());

Do you know how I can create and pass my CacheManager to sessionFactory?


Regards,
Niks

Niks

unread,
Mar 29, 2016, 7:52:39 AM3/29/16
to ehcache-users
Hi Fabien,

Another update:

With all configuration mentioned in my last post, 
when I tried to print CacheManager names with a test program as below:

CacheManager manager1 = CacheManager.create("DatabaseA-ehcache.xml");
String name1 = manager1.getName();
CacheManager manager2 = CacheManager.create("DatabaseB-ehcache.xml");
String name2 = manager2.getName();

Logs show both names still same, inspite of configuring different names in ehcache.xml

 name1  __DEFAULT__ 
 name2  __DEFAULT__ 

-Niks

On Tuesday, March 22, 2016 at 5:22:15 PM UTC, Niks wrote:

Niks

unread,
Mar 29, 2016, 8:41:46 AM3/29/16
to ehcache-users
Correction in last post:
---------------------------------------
Hi Fabien,

Another update:

With all configuration mentioned in my last post, 
when I tried to print CacheManager names with a test program as below:

CacheManager manager1 = CacheManager.create("DatabaseA-ehcache.xml");
String name1 = manager1.getName();
CacheManager manager2 = CacheManager.create("DatabaseB-ehcache.xml");
String name2 = manager2.getName();

Logs show both names still same, inspite of configuring different names in ehcache.xml

 name1 DatabaseACacheManager 
 name2 DatabaseACacheManager -- This should print as DatabaseBCacheManager . This may be the cause.

-Niks

On Tuesday, March 22, 2016 at 5:22:15 PM UTC, Niks wrote:

Fabien Sanglier

unread,
Apr 4, 2016, 10:45:28 AM4/4/16
to ehcach...@googlegroups.com
hey sorry for late reply on this...

CacheManager.create(); create a singleton CacheManager as explained in the doc: http://www.ehcache.org/apidocs/2.8.4/net/sf/ehcache/CacheManager.html#create(java.lang.String)
So what you see is actually perfectly normal and expected (on your 2nd create() call, it return the first one you created because of the singleton pattern)

If you were to use the CacheManager.newInstance() call instead, then you'd create a new cache manager per unique cache manager name...as explained in http://www.ehcache.org/apidocs/2.8.4/net/sf/ehcache/CacheManager.html#newInstance(java.lang.String)

Finally, in terms of why hibernate does not seem to find the right config (which I think that's what's going on here)...make sure the "net.sf.ehcache.configurationResourceName" value identify the right resource in the classpath since the resource is searched for in the root of the classpath.

net.sf.ehcache.configurationResourceName=/name_of_ehcache.xml

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Fabien Sanglier
fabiens...@gmail.com
Reply all
Reply to author
Forward
0 new messages