[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Using EhCache witn Hibernate and in my application  XML
Forum Index -> Ehcache
Author Message
monkiki

neo

Joined: 03/26/2012 12:36:07
Messages: 6
Offline

Hi there.

I have configured Hibernate to make use of EhCache. I want also use EhCache to cache some resources directly from my application.

The problem I have is that Hibernate has its CacheManager and my application has another one which collision with the first one because both use the same ehcache.xml configuration.

Do I need to create another configuration file for my CacheManager or can I get a reference to the CacheManager used by Hibernate? I have no seen a way to access this CacheManager instance.

Thanks in advance.
abroszni

journeyman

Joined: 12/13/2010 17:02:58
Messages: 12
Offline

You should be able to use the same ehcache.xml as the one used by hibernate.

Code:
 CacheManager cacheManager = CacheManager.create(getClass().getResource("your-ehcache-config.xml"));
 
 ....
 
 cacheManager.shutdown();
 
monkiki

neo

Joined: 03/26/2012 12:36:07
Messages: 6
Offline

Yes, will take the same EhCache configuration XML, but the EhCacheRegionFactory class does not create the CacheManager with the "create" method (which will be fine), but:

Code:
Configuration configuration = ConfigurationFactory.parseConfiguration();
 manager = new CacheManager(configuration);


And this method does not act as a singleton, and two CacheManager instances will be create: one for Hibernate and another for my application.
abroszni

journeyman

Joined: 12/13/2010 17:02:58
Messages: 12
Offline

You'll access to the same Caches instances

Try:
Code:
 ((EhcacheEntityRegion)((SessionFactoryImpl)sessionFactory).getAllSecondLevelCacheRegions()
         .get("com.pany.YourEntity")).getEhcache()
 
 and
 
 CacheManager.create(getClass().getResource("your-ehcache-config.xml")).getEhcache("com.pany.YourEntity")
 


You should access the same cache instance.
abroszni

journeyman

Joined: 12/13/2010 17:02:58
Messages: 12
Offline

Or, to answer your original question:

Code:
 ((EhcacheEntityRegion)((SessionFactoryImpl)sessionFactory).getAllSecondLevelCacheRegions()
       .get("com.pany.YourEntity")).getEhcache().getCacheManager()
 


There might be an easier way. that's the first one which come out of my head.
monkiki

neo

Joined: 03/26/2012 12:36:07
Messages: 6
Offline

Thanks a lot

I don't understand why accessing the CacheManager used by Hibernate is so "hidden". Using CacheManager.create() from Hibernate would be easier to access to the same CacheManager instance.

Regards.
hhuynh

ophanim

Joined: 06/16/2006 11:54:06
Messages: 736
Offline

I would guess your application own cache manager would have a different lifecycle than that of Hibernate. Especially, when you or Hibernate call cacheManger.shutdown(), you don't want to step on each other toes. It would make more sense to have your own cache manager in that case?
monkiki

neo

Joined: 03/26/2012 12:36:07
Messages: 6
Offline

I understand what do you mean, but in this case the lifecycle of my application match the one from Hibernate, so I don't see necessary to have two EhCache configuration files.
hhuynh

ophanim

Joined: 06/16/2006 11:54:06
Messages: 736
Offline

Hence the design from the Ehcache team. It should NOT be easy to get in the way of Hibernate operations by chance. But it's possible to get in its way with a little wiring.

The global CacheManager.ALL_CACHE_MANAGERS gives you a list of all managers which would be another way of picking out the cache manager that Hibernate is using.
monkiki

neo

Joined: 03/26/2012 12:36:07
Messages: 6
Offline

Interesting! I will take a look at CacheManager.ALL_CACHE_MANAGERS. The "problem" with Hibernate use of EhCache is that the manage the CacheManager singleton himself and not make use of the singleton provided by EhCache.
abroszni

journeyman

Joined: 12/13/2010 17:02:58
Messages: 12
Offline

Do not forget that Ehcache can be used as an in-memory cache and as a second level cache for hibernate.
If you plan to do both in your application, I would advise to use two different cache configuration files.
The reason behind that is that even though you could hold all your caches definitions in one file, I would find it easier to separate them, since they will not be used for the same functionalities.
It will also be easier to tune, with ARC (auto resource control) you could have your two different CacheManagers using different portions of the memory.

Look at http://ehcache.org/documentation/configuration/cache-size
section 'Pooled resources'

Code:
 <ehcache xmlns...
         Name="HibernateCaches"
         maxBytesLocalHeap="100M"
         maxBytesLocalOffHeap="10G"
         maxBytesLocalDisk="50G">
 ...
 
 <cache name="Entity1" ... </cache>
 <cache name="Entity2" ... </cache>
 <cache name="Entity3" ... </cache>
 
 </ehcache>
 
 and
 
 <ehcache xmlns...
         Name="InMemoryCaches"
         maxBytesLocalHeap="100M"
         maxBytesLocalOffHeap="10G"
         maxBytesLocalDisk="50G">
 ...
 
 <cache name="Cache1" ... </cache>
 <cache name="Cache2" ... </cache>
 <cache name="Cache3" ... </cache>
 
 </ehcache>
 
 

monkiki

neo

Joined: 03/26/2012 12:36:07
Messages: 6
Offline

Ok, the recommended way is to have two EhCache configuration: one for Hibernate and other for my own cache. I was not sure if my initial idea was right, but now I can see the light :)

Thanks for the clarification.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team