11

While integrating two subsystems, we were forced to use multiple SessionFactory instances, which lead to trouble when interacting with our Hibernate second-level cache (Terracotta EhCache). Specifically:

 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
  }        

The debug print-out shows STATUS_ALIVE for cache "Foo" but the call to size() throws an IllegalStateException:

java.lang.IllegalStateException: The Foo Cache is not alive.

Currently, both SessionFactories are configured to use SingletonEhCacheRegionFactory. If I switch the SessionFactories to use EhCacheRegionFactory (non-singleton), what are the ramifications for cache behavior (specifically in a Web App context)?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Larry OBrien
  • 8,484
  • 1
  • 41
  • 75
  • You are using two session factories, but inside the same app? Are the factories similar, or are they configured separately? – jpkroehling Dec 23 '10 at 10:37
  • Yes. We are migrating from an existing data model to a shiny new data model and we have to deal with both when integrating with legacy systems. So they're "similar" but not identical. – Larry OBrien Dec 23 '10 at 19:06
  • not sure if this helps: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3450 – Brad Cupit Jul 07 '11 at 19:11
  • This link looks interesting. http://stackoverflow.com/a/3690212/507864 – ManuPK Feb 22 '12 at 04:05

1 Answers1

7

EhCache will ensure that all instances of SingletonEhCacheRegionFactory use the same actual CacheManager internally, no matter how many instances of SingletonEhCacheRegionFactory you create, making it a crude version of the Singleton design pattern.

The plain EhCacheRegionFactory, on the other hand, will get a new CacheManager every time.

If you have two Hibernate session factories in Spring, each using their own instance of SingletonEhCacheRegionFactory, then they're actually going to end up sharing a lot of their cache state, which may account for your problem.

This isn't really a good match for Spring, where the singletons are supposed to be managed by the container. If you use EhCacheRegionFactory, then you'll likely get more predictable results. I suggest giving it a go and see how you get on.

skaffman
  • 398,947
  • 96
  • 818
  • 769