| Author |
Message |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/26/2012 12:42:28
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/26/2012 13:13:09
|
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();
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/26/2012 13:40:30
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/26/2012 13:50:19
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/26/2012 13:52:31
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/27/2012 00:23:50
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/27/2012 08:16:52
|
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?
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/27/2012 09:56:59
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/27/2012 10:07:08
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/27/2012 10:35:48
|
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.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/27/2012 15:15:42
|
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>
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 03/28/2012 15:49:15
|
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.
|
|
|
 |
|
|