一尘不染

同一个虚拟机中已存在另一个未命名的缓存管理器(ehCache 2.5)

spring

这是我运行junit测试时发生的情况…

Another CacheManager with same name 'cacheManager' already exists in the same VM. Please 
provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same
   CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.

The source of the existing CacheManager is: 
 DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]

异常背后的原因是什么?可以同时运行1个以上的cacheManager吗?

这就是我使用Sping 3.1.1配置cachManager的方式。它将cacheManager的范围明确设置为“单例”

<ehcache:annotation-driven />

<bean
    id="cacheManager"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    scope="singleton"
    />

The ehcache.xml looks like

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
     updateCheck="false"
     maxBytesLocalHeap="100M" 
     name="cacheManager"
     >
 ....
 </ehcache>

Finally my class

@Component
public class BookingCache implements CacheWrapper<String, BookingUIBean> {

     @Autowired
     private CacheManager ehCacheManager;
      ....
}

我非常确定我在代码库中只处理一个cacheManager。其他一些可能正在运行第n个实例。


阅读 665

收藏
2020-04-15

共2个答案

一尘不染

你的EhCacheManagerFactoryBean可能是一个单例,但它正在构建多个CacheManager并尝试为其赋予相同的名称。这违反了Ehcache 2.5 语义。

`Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM.

Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule`

通过将shared属性设置为true,告诉工厂bean在JVM中创建CacheManager的共享实例。

<bean id="cacheManager"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
      p:shared="true"/>
2020-04-15
一尘不染

使用JPA(2.0)+ Hibernate(3.6.4)+ Spring(3.2.4)进行集成测试时,我遇到了同样的问题。使用以下Hibernate配置解决了该问题:

<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>

而不是使用

<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
2020-04-15