Ehcache关闭时在运行测试套件时引发异常

21

我遇到了以下问题。

我的项目中有一个测试套件,每个测试都单独运行正常。

但是当我将它们作为套件运行时,一些测试会失败并出现以下异常:

Caused by: java.lang.IllegalStateException: The dao Cache is not alive (STATUS_SHUTDOWN)
    at net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:4269)
    at net.sf.ehcache.Cache.checkStatus(Cache.java:2703)
    at net.sf.ehcache.Cache.get(Cache.java:1576)
    at org.springframework.cache.ehcache.EhCacheCache.get(EhCacheCache.java:61)
    at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheables(CacheAspectSupport.java:310)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:198)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

有没有办法避免这种行为,即在多个测试之间保持缓存的有效性或正常关闭它?


4
在测试环境中如何将共享属性设置为 false?能否举个例子? - Luigi Saggese
5个回答

6
尝试在测试环境中将EhCacheManagerFactoryBeanEhCacheCacheManager的共享属性设置为false。

但是这个配置也可以实现吗?<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider"/> <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" /> - Luigi Saggese
2
在测试环境中如何将共享属性设置为false,能否提供一个例子? - Osama Abdulsattar

2
为测试单独创建缓存配置,并将作用域设置为“prototype”。
@Configuration
@EnableCaching
public class EhCacheConfig {

 @Bean(name = "cacheManager")
 @Scope("prototype")
 public CacheManager getCacheManager() {
    return new EhCacheCacheManager(getEhCacheFactory().getObject());
 }

 @Bean
 @Scope("prototype")
 public EhCacheManagerFactoryBean getEhCacheFactory() {
    EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
    factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
    factoryBean.setShared(true);
    return factoryBean;
 }
}

1

在你的失败测试类上加上@AutoConfigureCache注解。 默认情况下,此注解将安装一个NoOpCacheManager,即一个基本的、无操作的CacheManager实现,适用于禁用缓存,通常用于支持没有实际后备存储的缓存声明。 它将简单地接受任何项目进入缓存而不实际存储它们。

Spring文档关于@AutoConfigureCache

Spring文档关于@NoOpCacheManager


只有Spring Boot应用程序才能实现。 - Simon Logic
是的,它适用于使用Spring Boot进行测试,谢谢。 - undefined

0

0

这个问题基本上发生在多个应用程序之间共享缓存时。 因此,请尝试通过将共享属性设置为false来避免共享缓存。

<spring:bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <spring:property name="configLocation" value="classpath:ehcache.xml" /> <spring:property name="shared" value="false" /> </spring:bean>

但是在执行过程中,您会遇到

同一VM中已经存在具有相同名称“cacheManager”的另一个CacheManager。 IllegalStateException

为了解决这个问题,我们需要提到

<spring:property name="cacheManagerName" value="abc" />

我希望最终问题得到解决。


添加名称不会删除已经存在于同一VM中的'cacheManager'。 IllegalStateException异常。 - Sid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接