2013-04-29 68 views
9

我遇到了以下問題。Ehcache在運行測試套件時會導致異常

我在我的項目中有一個測試套裝,每個測試都運行良好。

然而,當我運行它們作爲套件我一些他們的失敗,出現以下異常:

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) 

是否有辦法避免這種行爲,即保持高速緩存活在多個測試或關機正常嗎?

+2

如何可以在測試上下文中將共享屬性設置爲false,可以提供一個示例? – 2013-08-13 10:19:10

回答

4

嘗試在測試環境中的EhCacheManagerFactoryBeanEhCacheCacheManager中將共享屬性設置爲false。

+0

這樣做的技巧 – genjosanzo 2013-05-08 16:31:43

+0

但是,這種配置也有可能嗎? ' \t \t ' – 2013-08-13 10:16:45

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>

但在執行,你會遇到

同名 '的CacheManager' 已經存在於同一個VM的另一個的CacheManager。 IllegalStateException異常

爲了解決這個問題,我們必須要提到

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

我希望最後的問題將得到解決。

+0

添加名稱不會刪除已存在於同一虛擬機中的'cacheManager'。 IllegalStateException異常 – Sid 2017-01-10 09:31:48

1

爲測試做一個單獨的緩存配置!並把範圍「原型」

@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; 
} 
} 
相關問題