title: Error : "Another unnamed CacheManager already exists in the same VM" Using Spring, Jersey and ehCache
body: <p>There seems to be another process loading the cache, but not able to locate this (that's if the error isn't telling me lie). The only thing its telling me is that i have another cache started somewhere from a class called <strong>InputStreamConfigurationSource</strong> ???</p>
  
  <p>Has anyone come across this error ???</p>
  
  <p>I am using</p>
  
  <ul>
  <li>Spring 3</li>
  <li>Jersey 1.6/1.7</li>
  <li>ehCache 2.6</li>
  </ul>
  
  <p>StackTrace</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/FileService] threw exception [com.sun.jersey.api.container.ContainerException: Unable to create resource class com.myapp.FileStoreAccessAction] with root cause
  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@e782a8]
  at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:521)
  at net.sf.ehcache.CacheManager.init(CacheManager.java:371)
  at net.sf.ehcache.CacheManager.&lt;init&gt;(CacheManager.java:356)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
  at com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:191)
  at com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:179)
  at com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
  at com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
  at com.sun.jersey.server.impl.application.WebApplicationImpl$19$1.getValue(WebApplicationImpl.java:1032)
  at com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable$1.getValue(AbstractHttpContextInjectable.java:104)
  at com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:196)
  at com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:179)
  at com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
  at com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
  at com.sun.jersey.server.impl.application.WebApplicationImpl$19$1.getValue(WebApplicationImpl.java:1032)
  at com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable$1.getValue(AbstractHttpContextInjectable.java:104)
  at com.sun.jersey.server.spi.component.ResourceComponentInjector.inject(ResourceComponentInjector.java:222)
  at com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:180)
  at com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
  at com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
  at com.sun.jersey.server.impl.application.WebApplicationContext.getResource(WebApplicationContext.java:238)
  at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
  at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
  at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
  at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480)
  at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411)
  at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
  at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
  at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
  at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
  at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p><strong>Spring Config</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  
  &lt;!-- Enable annotation configuration --&gt;
  &lt;context:annotation-config/&gt;
  
  &lt;context:component-scan base-package="com.myapp" /&gt;
  
  &lt;!-- Jackson JSON ObjectMapper --&gt;
  &lt;bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper" /&gt;
  
  &lt;!-- Cache Manager --&gt;
  &lt;bean id="ehCacheManager"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" scope="singleton"&gt;
      &lt;property name="configLocation" value="classpath:cacheConfig.xml" /&gt;
      &lt;property name="shared" value="true"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- Data Access --&gt;
  &lt;bean id="dataAccessFactory" class="com.myapp.DataAccessFactory" /&gt;
  
  &lt;!-- Application Stuff --&gt;
  &lt;bean id="fileStoreService" class="com.myapp.FileStoreService" scope="singleton"&gt;
      &lt;constructor-arg ref="dataAccessFactory"/&gt;
      &lt;constructor-arg ref="ehCacheManager" /&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Web xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5"&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/classes/log4j.properties&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:appContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;SpringDispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;SpringDispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>Cache file</strong></p>
  
  <pre><code>&lt;ehcache&gt;
  &lt;cache name="FileStoreList" maxElementsInMemory="10000" eternal="false"
      overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="0"
      diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /&gt;
  &lt;/ehcache&gt;
  </code></pre>
  
  <p><strong>File Service Class</strong></p>
  
  <pre><code>public class FileStoreService {
  
  private final CacheManager cacheFactory;
  
  private final DataAccessFactory factory;
  
  public FileStoreService(@InjectParam DataAccessFactory factory, @InjectParam CacheManager ehCacheManager) {
      this.factory = factory;
      cacheFactory = ehCacheManager;
  }
  ...
  </code></pre>
  
  <p><strong>JSON End Point</strong></p>
  
  <pre><code>public class FileStoreAccessAction {
  
  /**
   * JSON Object Mapper - pasted in via Spring
   */
  @InjectParam
  private ObjectMapper mapper;
  
  @InjectParam()
  private FileStoreService service;
  
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response list() {
  
  ....
  </code></pre>