Monday, April 6, 2015

Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM.

When you run unit tests (strictly speaking, integration tests),

you can encounter the following exception:

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: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@6fd1660]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:628)
at net.sf.ehcache.CacheManager.init(CacheManager.java:392)

You can fix it by setting `shared` of `EhCacheManagerFactoryBean` to `true` as follows:

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

Reference:
http://stackoverflow.com/questions/10013288/another-unnamed-cachemanager-already-exists-in-the-same-vm-ehcache-2-5

No comments:

Post a Comment