问题描述
这是我运行junit测试时发生的情况...
This is what happens when I run my junit tests...
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吗?
What's the reason behind the exception. Could there be more than 1 cacheManager running simultaneously?
这是我使用Sping 3.1.1配置cachManager的方式.它将cacheManager的范围明确设置为单个"
This is how I configured the cachManager using Sping 3.1.1. It sets explicitly the scope of the cacheManager to "singleton"
<ehcache:annotation-driven />
<bean
id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
scope="singleton"
/>
ehcache.xml看起来像
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>
最后我的课
@Component
public class BookingCache implements CacheWrapper<String, BookingUIBean> {
@Autowired
private CacheManager ehCacheManager;
....
}
我非常确定我在代码库中仅处理一个cacheManager.其他一些可能正在运行第n个实例.
I'm very sure that I'm dealing with only one cacheManager in my code base. Something else is probably running the n-th instance.
推荐答案
您的EhCacheManagerFactoryBean可能是一个单例,但它正在构建多个CacheManager并尝试为其赋予相同的名称.这违反了Ehcache 2.5 语义.
Your EhCacheManagerFactoryBean may be a singleton, but it's building multiple CacheManagers and trying to give them the same name. That violates Ehcache 2.5 semantics.
Ehcache 2.5及更高版本不允许在同一个JVM中存在多个具有相同名称的CacheManager.创建非单一CacheManager的CacheManager()构造函数可能违反此规则
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.
Tell the factory bean to created a shared instance of the CacheManager in the JVM by setting the shared property to true.
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:shared="true"/>
这篇关于同一台VM(ehCache 2.5)中已经存在另一个未命名的CacheManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!