CacheException: Another unnamed CacheManager already exists in the same VM




Asked on March 16, 2015
Hi Friends,

I am configuring spring and ehcache using @Configuration annotation with @EnableCaching. I am getting a strange error. Please help me. Find the error.

Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:529)
at net.sf.ehcache.CacheManager.init(CacheManager.java:374)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:259)
at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.afterPropertiesSet(EhCacheManagerFactoryBean.java:157)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
 




Replied on March 16, 2015
Post your configuration class.


Replied on March 16, 2015
My configuration class is

@Configurable
@EnableCaching
public class CacheConfig {
@Bean
public Person getPerson(){
 return  new Person();
}
@Bean
public CacheManager getCacheManager(){
 return  new EhCacheCacheManager(getEhCacheFactory().getObject());
}
@Bean
public EhCacheManagerFactoryBean getEhCacheFactory(){
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
return factoryBean;
}
}



Replied on March 16, 2015
If creating instance of EhCacheManagerFactoryBean is not shared across classloader, we get the error as you mentioned. We need to make it singleton. Spring provides setShared() method using which we can make it shared by passing value true. Do the changes as

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




Replied on March 16, 2015
Thanks a lot, Arvind.

It resolved my error.


Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us