首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多个EntityManagerFactory的EhCache CacheManager

具有多个EntityManagerFactory的EhCache CacheManager
EN

Stack Overflow用户
提问于 2012-10-02 22:52:10
回答 2查看 5.7K关注 0票数 2

在spring-server.xml中有一个entityManagerFactory。

但是我必须再生成一个entityManager,并且我使用

代码语言:javascript
复制
Persistence.createEntityManagerFactory("myotherpersistenceunitname");

但我得到了例外

代码语言:javascript
复制
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:457)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:354)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:242)
    at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:70)

spring.xml:

代码语言:javascript
复制
<context:property-placeholder location="classpath:application.properties"/>
    <context:component-scan base-package="merve.web.app" >
     <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    </context:component-scan>
    <context:annotation-config/>
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"  />

    <cache:annotation-driven />


    <bean id="properties" class="merve.web.app.configuration.PropertyResourceConfiguration" />

    <bean id="entityManagerFactory"  
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
         <property name="persistenceUnitName" value="myPU"/>  
         <property name="dataSource" ref="dataSource" />  
         <property name="jpaVendorAdapter">  
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
                 <property name="databasePlatform" value="${database.target}"/>  
                 <property name="showSql" value="${database.showSql}" />  
             </bean>
         </property>  
    </bean>  

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${database.driver}"/>
        <property name="jdbcUrl" value="${database.url}"/>
        <property name="user" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="minPoolSize" value="2"/>
        <property name="maxPoolSize" value="10"/>
        <property name="breakAfterAcquireFailure" value="false"/>
        <property name="acquireRetryAttempts" value="3"/>
        <property name="idleConnectionTestPeriod" value="300" />
        <property name="testConnectionOnCheckout" value="true" />
    </bean>


    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>



    <bean id="jamesEntityManagerFactory"  
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
         <property name="persistenceUnitName" value="jamesPU"/>  
         <property name="dataSource" ref="dataSourceJames" />
         <property name="persistenceXmlLocation" value="classpath:META-INF/james-persistence.xml"/> 
         <property name="jpaVendorAdapter">  
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
                 <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>  
                 <property name="showSql" value="true" />  
             </bean>
         </property>   
    </bean>  



    <bean id="dataSourceJames" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="org.apache.derby.jdbc.EmbeddedDriver"/>
        <property name="jdbcUrl" value="jdbc:derby:../var/store/derby;create=true"/>
        <property name="user" value="app"/>
        <property name="password" value="app"/>
        <property name="minPoolSize" value="2"/>
        <property name="maxPoolSize" value="10"/>
        <property name="breakAfterAcquireFailure" value="false"/>
        <property name="acquireRetryAttempts" value="3"/>
        <property name="idleConnectionTestPeriod" value="300" />
        <property name="testConnectionOnCheckout" value="true" />
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  
        <property name="basenames">  
            <list>
                <value>messages</value>
            </list>
        </property>
    </bean>

    <!-- Ehcache library setup -->
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true" p:config-location="classpath:ehcache.xml"/>

    <bean id="cacheManager"   class="org.springframework.cache.ehcache.EhCacheCacheManager" >
     <property name="cacheManager"><ref local="ehcache"></ref></property>  
    </bean>

<dwr:configuration/>
    <dwr:annotation-scan base-package="tuxi.web.app.service.dwr" scanRemoteProxy="true" scanDataTransferObject="true"/>
    <dwr:url-mapping />

    <dwr:controller id="dwrController"/>

</beans>
EN

回答 2

Stack Overflow用户

发布于 2014-08-05 01:58:52

here中描述并在the source中修复的问题是没有正确使用EhCache单例。答案取决于您使用的spring-context-support版本和EhCache版本。对于这两者,您需要使用EhCache 2.6或更高版本的

代码语言:javascript
复制
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.6.0</version>
</dependency>

接下来,根据您的spring-context-support版本确定要执行的操作:

如果使用Spring 3.1/3.2

代码语言:javascript
复制
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:shared="true" 
    p:config-location="classpath:ehcache.xml"/>

如果使用Spring 4.x

代码语言:javascript
复制
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:shared="false"
    P:acceptExisting="true" 
    p:config-location="classpath:ehcache.xml"/>
票数 2
EN

Stack Overflow用户

发布于 2014-09-19 04:37:17

尝试在ehcache.xml中以不同的方式命名这两个cacheManagers

代码语言:javascript
复制
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    name="ehCacheManager1">

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    name="ehCacheManager2">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12692718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档