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

前端 未结 17 1934
囚心锁ツ
囚心锁ツ 2020-11-30 20:41

This is what happens when I run my junit tests...

Another CacheManager with same name \'cacheManager\' already exists in the same VM. Please 
provide unique          


        
相关标签:
17条回答
  • 2020-11-30 21:07

    I solved it by adding following to resources.groovy :

    beans = { ... aclCacheManager(EhCacheManagerFactoryBean) { shared = true } ... }

    0 讨论(0)
  • 2020-11-30 21:09

    Your problem is the context loading optimization built in the Spring test framework. Spring (per default) does not destroy the context once the test class is done, in hope that another test class might reuse it (instead of creating it from scratch).

    You can override this default using @DirtiesContext, or if you use maven you can set surefire forkMode to "always" and create a new VM per test class.

    0 讨论(0)
  • 2020-11-30 21:09

    In my case Problem is component-scan and java config.

    root-context.xml
    <context:component-scan base-package="org.beansugar">
    
    servlet-context.xml
    <context:component-scan base-package="org.beansugar">
    

    spring component-scan work two times on xml files. it generate beans inside SpringConfig.java each run time. then duplicate cache manager was created.

    so, I changed that like below.

    root-context.xml
    <context:component-scan base-package="org.beansugar">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    servlet-context.xml
    <context:component-scan base-package="org.beansugar" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    0 讨论(0)
  • 2020-11-30 21:12

    if you just test your business service,not second level cache,you can remove second level configuration in your spring config file,your test will be run successfully. there is my second level configuration :

     <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="persistenceUnitName" value="defaultPU" />
            <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                    <prop key="hibernate.cache.use_second_level_cache">false</prop>
                    <prop key="hibernate.cache.use_query_cache">false</prop>
                </props>
            </property>
        </bean>
    

    if i change to full configuration of second level cache config ,the real webapp use in running time,like this:

        <bean id="entityManagerFactory"
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="persistenceUnitName" value="defaultPU" />
                <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
                <property name="jpaProperties">
                    <props>
                        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                        <prop key="hibernate.show_sql">false</prop>
                        <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                        <prop key="hibernate.cache.use_second_level_cache">true</prop>
                        <prop key="hibernate.cache.use_query_cache">true</prop>
                        <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>               
                        <prop key="net.sf.ehcache.configurationResourceName">ehcache/ehcache-hibernate-local.xml</prop>
                    </props>
                </property>
            </bean>
    

    then i get the same Exception "Another unnamed CacheManager already exists in the same VM"

    0 讨论(0)
  • 2020-11-30 21:14

    This error also happens with wrong mapping files. The message is horrible, doesn't say the cause.

    0 讨论(0)
  • 2020-11-30 21:19

    You may also try to set name"xxx" on your ehcache.xml configuration (on the ehcache element).

    That did the trick for me, as I think I had another cache configuration lurking in one of the modules of my app.

    The shared solution also works, but I don't know the far-ranging implications of that.

    • http://forums.terracotta.org/forums/posts/list/6495.page
    • https://norrisshelton.wordpress.com/2012/03/08/spring-3-1-caching-abstraction-with-ehcache/
    0 讨论(0)
提交回复
热议问题