Ehcache shutdown causing an exception while running test suite
Asked Answered
C

5

21

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?

Curtcurtail answered 29/4, 2013 at 14:54 Comment(1)
How it's possible to set shared property to false in testing context, it's possible to provide an example?Gaige
E
6

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

Ephemerality answered 4/5, 2013 at 3:37 Comment(2)
But it's also possible with that configuration? <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" />Gaige
How it's possible to set shared property to false in testing context, is it possible to provide an example?Spritsail
F
2

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;
 }
}
Finedrawn answered 11/7, 2017 at 9:6 Comment(0)
M
1

Annotate your failing test class with @AutoConfigureCache. By default, this annotation will install a NoOpCacheManager, i.e. a basic, no operation CacheManager implementation suitable for disabling caching, typically used for backing cache declarations without an actual backing store. It will simply accept any items into the cache not actually storing them.

Spring Documentation on @AutoConfigureCache

Spring Documentation on @NoOpCacheManager

Mammilla answered 15/1, 2021 at 7:51 Comment(2)
it is possible for spring boot app only.Mcspadden
yes it works for witn Spring boot tests, thanksExfoliation
W
0

JUnit share Spring context for speed. I've avoid from this exception when remove explicitly Spring context closing in one of my test. See Reuse spring application context across junit test classes

Widen answered 12/4, 2015 at 20:36 Comment(0)
C
0

This issue occurs basically, Whenever you Cache shared among multiple applications. So try not to share your cache by setting shared property to 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>

But on execution you will encounter

Another CacheManager with same name 'cacheManager' already exists in the same VM. IllegalStateException

To counter this we need to mention

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

I hope finally issue will be resolved.

Cauline answered 24/11, 2015 at 4:24 Comment(1)
Adding the name does not remove the 'cacheManager' already exists in the same VM. IllegalStateException exceptionPyrophoric

© 2022 - 2024 — McMap. All rights reserved.