Another CacheManager with same name ” already exists in the same VM while application starts

net.sf.ehcache.CacheException: Another CacheManager with same name 'fernowebapp' already exists in the same VM. Please provide unique names for each CacheManage
r 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.

This is the web.xml

 <servlet>
    <servlet-name>ferno</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
<!--         <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
    <servlet-name>ferno</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/ferno-servlet.xml</param-value>
</context-param>
<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.vuelogix.collygo.context.FernoApplicationContextInitializer</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

When i inspected the log , i found the application context is initialized twice. I just verified it by disabling the load on start up for dispatcher servlet. When i disable the dispatcher servlet , i found the exception doesnt comes at the time of start up. But it occurs when i first try to hit any controller class.

i found few similar issues in Why does Spring MVC need at least two contexts? , Spring – application Initialized twice?

in the link below its mentioned that for Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule.

Another unnamed CacheManager already exists in the same VM (ehCache 2.5)

If the application context is to be loaded again for initializing dispatcher servlet, What is the best way to initialize classes like CacheManager ?

I believe it will work if i add init params for dispatcher servlet. mentioned in comment. In my case, I dont have a seperate application context for dispatcher servlet, So i believe it works fine in my application context. Update: So , when i really need a seperate application context for my dispatcher servlet, the best practice will be to remove the cache declaration(in this case) from the second context and make those configuration on root config? Correct me if i am wrong

Answer

You have almost found the answer with Why does Spring MVC need at least two contexts? question.

As you are using Spring MVC, you have the root context and the dispatcher servlet context that uses the root as its parent.

But here with :

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/ferno-servlet.xml</param-value>
</context-param>

you declare that root context should be loaded from /WEB-INF/ferno-servlet.xml, and as you named your servlet ferno, the DispatcherServlet context is loaded from same file. You are then creating each bean twice once in each context – that’s at least dangerous …

So you must declare 2 contextes and :

  • either
    • put in root one : all what in not related to dispatcher servlet : model, persistence beans, filters, etc.
    • put in dispatcher servlet one : controlers, views, interceptors, etc.
  • or put everything in root context and leave the other empty. This can work because root context is the parent of dispatcher servlet one, and as such Spring MVC looks in root context beans not found in second one.

And never, ever use a name like xxx-servlet.xml for root application context to avoid this kind of problem.

Leave a Reply

Your email address will not be published. Required fields are marked *