Java – what is the impact of using singletonehcacheregionfactory and ehcacheregionfactory in web applications on Hibernate Level 2 cache?
When integrating the two subsystems, we are forced to use multiple sessionfactory instances, which will cause problems when interacting with our hibernate L2 cache especially:
for(CacheManager cm : CacheManager.ALL_CACHE_MANAGERS){ LOGGER.log(Level.DEBUG,"In cm " + cm.getName()); for(String cn : cm.getCacheNames()){ LOGGER.log(Level.DEBUG,"I have a cache called " + cn); LOGGER.log(Level.DEBUG,"it's status is " + ms.getCache(cn).getStatus()); } } try{ myCollection.size(); }catch(IllegalStateException ise){ LOGGER.log(Level.FATAL,ise); //Triggered }
Debug the printout to display the status of cache "foo"_ Alive, but calling size() will throw IllegalStateException:
java.lang.IllegalStateException: The Foo Cache is not alive.
Currently, sessionfactories are configured to use singletonehcacheregionfactory If I switch sessionfactories to use ehcacheregionfactory (not a singleton), what are the consequences of caching behavior (especially in the context of web app)?
Solution
Ehcache will ensure that all instances of singletonehcacheregionfactory internally use the same actual CacheManager, no matter how many singletonehcacheregionfactory instances you create, making them bold versions of the singleton design pattern
On the other hand, ehcacheregionfactory gets a new CacheManager every time
If there are two hibernate session factories in spring, each of which uses its own singletonehcacheregionfactory instance, in fact, they will eventually share many cache states, which may be your problem
This is not a good match because singles should be managed by containers If you use ehcacheregionfactory, you may get more predictable results I suggest you go and see how you get there