【问题标题】:Ehcache shutdown causing an exception while running test suite运行测试套件时 Ehcache 关闭导致异常
【发布时间】:2013-04-23 07:19:58
【问题描述】:

我遇到了以下问题。

我的项目中有一套测试服,每个单独的测试都运行良好。

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

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)

有没有办法避免这种行为,即在多个测试中保持缓存处于活动状态或正确关闭它?

【问题讨论】:

  • 如何在测试环境中将共享属性设置为false,可以举个例子吗?

标签: spring junit4 ehcache spring-test


【解决方案1】:

尝试在测试上下文中将 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" />
  • 如何在测试环境中将共享属性设置为false,可以提供一个例子吗?
【解决方案2】:

只为测试做一个单独的缓存配置!并把范围“原型”

@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;
 }
}

【讨论】:

    【解决方案3】:

    这个问题基本上会发生,每当您在多个应用程序之间共享缓存时。 所以尽量不要通过将 shared 属性设置为 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>

    但在执行时你会遇到

    同一个虚拟机中已经存在另一个同名“cacheManager”的缓存管理器。 IllegalStateException

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

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

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

    【讨论】:

    • 添加名称不会删除同一 VM 中已存在的“cacheManager”。 IllegalStateException 异常
    【解决方案4】:

    JUnit 共享 Spring 上下文以提高速度。 在我的一个测试中明确删除 Spring 上下文关闭时,我避免了这个异常。见Reuse spring application context across junit test classes

    【讨论】:

      【解决方案5】:

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

      Spring Documentation on @AutoConfigureCache

      Spring Documentation on @NoOpCacheManager

      【讨论】:

      • 只有spring boot app才有可能。
      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      相关资源
      最近更新 更多