Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ehcache shutdown causing an exception while running test suite

I'm experiencing the following problem.

I have a test suit in my project and each individual test runs fine.

However when I run them as a suite I some of them fails with the following exception:

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)

Is there a way to avoid this behavior, i.e. keep the cache alive across multiple test or shutting it down properly?

like image 364
genjosanzo Avatar asked Apr 29 '13 14:04

genjosanzo


People also ask

How do I disable ehcache?

sf. ehcache. disabled=true in the Java command line) disables caching in ehcache. If disabled, no elements can be added to a cache (puts are silently discarded).

Is ehcache in memory cache?

1. Overview. In this article, we will introduce Ehcache, a widely used, open-source Java-based cache. It features memory and disk stores, listeners, cache loaders, RESTful and SOAP APIs and other very useful features.

Is ehcache thread safe?

It is inherently not thread-safe to modify the value. It is safer to retrieve a value, delete the cache element, and then reinsert the value.


2 Answers

try to set shared property to false in EhCacheManagerFactoryBean or EhCacheCacheManager in the testing context.

like image 134
Bassem Reda Zohdy Avatar answered Sep 23 '22 02:09

Bassem Reda Zohdy


Make a seperate cache config for tests only! and put scope "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;
 }
}
like image 43
R.A Avatar answered Sep 22 '22 02:09

R.A