简体   繁体   English

在Web应用程序中使用SingletonEhCacheRegionFactory和EhCacheRegionFactory对Hibernate二级缓存有什么影响?

[英]What are the implications of using SingletonEhCacheRegionFactory vs. EhCacheRegionFactory for Hibernate 2nd-level cache in a Web Application?

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). 在集成两个子系统时,我们被迫使用多个SessionFactory实例,这在与我们的Hibernate二级缓存(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 : 调试打印输出显示缓存“Foo”的STATUS_ALIVE ,但调用size()会抛出IllegalStateException

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

Currently, both SessionFactories are configured to use SingletonEhCacheRegionFactory . 目前,两个SessionFactories都配置为使用SingletonEhCacheRegionFactory If I switch the SessionFactories to use EhCacheRegionFactory (non-singleton), what are the ramifications for cache behavior (specifically in a Web App context)? 如果我将SessionFactories切换为使用EhCacheRegionFactory (非单例),缓存行为的后果是什么(特别是在Web App上下文中)?

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. EhCache将确保SingletonEhCacheRegionFactory所有实例在内部使用相同的实际CacheManager ,无论您创建多少SingletonEhCacheRegionFactory实例,使其成为Singleton设计模式的粗略版本。

The plain EhCacheRegionFactory , on the other hand, will get a new CacheManager every time. 另一方面,普通的EhCacheRegionFactory每次都会获得一个新的CacheManager

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. 如果你在Spring中有两个Hibernate会话工厂,每个工厂都使用自己的SingletonEhCacheRegionFactory实例,那么它们实际上最终会分享很多缓存状态,这可能会解决你的问题。

This isn't really a good match for Spring, where the singletons are supposed to be managed by the container. 这对于Spring来说并不是一个很好的匹配,因为Spring应该由容器管理单例。 If you use EhCacheRegionFactory , then you'll likely get more predictable results. 如果您使用EhCacheRegionFactory ,那么您可能会获得更可预测的结果。 I suggest giving it a go and see how you get on. 我建议你试一试,看看你是怎么过的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM