本文介绍了同一个 VM 中已经存在另一个未命名的 CacheManager (ehCache 2.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我运行 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 配置缓存管理器的方式.它明确地将 cacheManager 的范围设置为singleton"

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.

2.5 之前的 Ehcache 版本允许在 JVM 中存在任意数量的同名(相同配置资源)的 CacheManager.

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 属性为真.

<bean id="cacheManager"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
      p:shared="true"/>

这篇关于同一个 VM 中已经存在另一个未命名的 CacheManager (ehCache 2.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 03:56