【问题标题】:How to configure Caching for Multiple EntityManagers with ehcache.xml and terracotta如何使用 ehcache.xml 和 terracotta 为多个 EntityManager 配置缓存
【发布时间】:2014-11-25 04:48:45
【问题描述】:

配置:- 给定多个实体管理器和单个 CacheManager 的配置与陶土。但是当我们启动显示 Connected Clients(0) 的 terracotta 服务器时,我们无法看到使用 terracotta 的缓存。因此,如果在 ehcache.xml 文件中发现任何问题,请检查此配置,请告诉我。

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xmlns:security="http://www.springframework.org/schema/security"

    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/data/jpa 
            http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/security 
            http://www.springframework.org/schema/security/spring-security-3.1.xsd
            http://www.springframework.org/schema/jdbc 
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">


    <!-- Will automatically be transactional due to @Transactional. EntityManager 
        will be auto-injected due to @PersistenceContext. PersistenceExceptions will 
        be auto-translated due to @Repository. -->

    <cache:annotation-driven cache-manager="cacheManager"/>

    <bean id="defaultDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driver.classname}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxConnectionAge" value="240" />
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20">
        </property>
        <property name="checkoutTimeout" value="20000" /><!-- Give up 
            waiting for a connection after this many milliseconds -->
        <property name="maxIdleTime" value="3000" />
        <property name="idleConnectionTestPeriod" value="100" />
    </bean>


    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

    <bean id="entityManagerFactory" parent="abstractEntityManagerFactory">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="tenantPU" />
        <property name="packagesToScan" value="com.qmplus.entity" />
        <property name="jpaProperties">
            <map>
                <entry key="hibernate.dialect" value="${hibernate.dialect}"></entry>
                <entry key="hibernate.ejb.naming_strategy" value="${hibernate.ejb.naming_strategy}"></entry>
                <entry key="hibernate.format_sql" value="${hibernate.format_sql}"></entry>
                <!-- HBMDDL2 doesn't work in multitenant environment -->
                <!-- <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"></entry> -->
                <entry key="hibernate.show_sql" value="${hibernate.show_sql}"></entry>
                <!-- <entry key="hibernate.hbm2ddl.import.files" value="classpath:import.sql"></entry> 
                    <entry key="hibernate.hbm2ddl.import_files_sql_extractor" value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor"></entry> -->
                <entry key="hibernate.enable_lazy_load_no_trans" value="true"></entry>

                <!-- Start EhCache Configuration -->
                <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"></entry>
                <entry key="hibernate.cache.use_second_level_cache" value="true"></entry>
                <entry key="hibernate.cache.use_query_cache" value="true"></entry>
                <entry key="hibernate.cache.provider_configuration_file_resource_path" value="classpath:ehcache.xml"></entry>
                <!-- End EhCache Configuration -->

                <entry key="hibernate.cache.generate_statistics" value="false"></entry>
                <entry key="hibernate.multiTenancy" value="DATABASE"></entry>
                <entry key="hibernate.tenant_identifier_resolver" value-ref="multiTenantIdentifierResolver"></entry>
                <entry key="hibernate.multi_tenant_connection_provider"
                    value-ref="multiTenantConnectionProvider"></entry>
            </map>

        </property>

    </bean>

    <bean id="controlEntityManagerFactory" parent="abstractEntityManagerFactory">
        <property name="dataSource" ref="defaultDataSource" />
        <property name="persistenceUnitName" value="controlPU" />
        <property name="packagesToScan" value="com.qmplus.control.entity" />
        <property name="jpaProperties">
            <map>
                <entry key="hibernate.dialect" value="${hibernate.dialect}"></entry>
                <entry key="hibernate.ejb.naming_strategy" value="${hibernate.ejb.naming_strategy}"></entry>
                <entry key="hibernate.format_sql" value="${hibernate.format_sql}"></entry>
                <!-- HBMDDL2 doesn't work in multitenant environment -->
                <!-- <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"></entry> -->
                <entry key="hibernate.show_sql" value="${hibernate.show_sql}"></entry>
                <!-- <entry key="hibernate.hbm2ddl.import.files" value="classpath:import.sql"></entry> 
                    <entry key="hibernate.hbm2ddl.import_files_sql_extractor" value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor"></entry> -->
                <entry key="hibernate.enable_lazy_load_no_trans" value="true"></entry>

                <!-- Start EhCache Configuration -->
                <!-- <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"></entry>
                <entry key="hibernate.cache.use_second_level_cache" value="true"></entry>
                <entry key="hibernate.cache.use_query_cache" value="true"></entry>
                <entry key="hibernate.cache.provider_configuration_file_resource_path" value="classpath:ehcache.xml"></entry> -->
                <!-- End EhCache Configuration -->

                <!-- <entry key="hibernate.cache.generate_statistics" value="false"></entry>
                <entry key="hibernate.multiTenancy" value="SCHEMA"></entry>
                <entry key="hibernate.tenant_identifier_resolver" value-ref="multiTenantIdentifierResolver"></entry>
                <entry key="hibernate.multi_tenant_connection_provider"
                    value-ref="multiTenantConnectionProvider"></entry> -->

            </map>
        </property>



    </bean>


    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cacheManager-ref="ehcache" />
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:configLocation="classpath:ehcache.xml" p:shared="true" p:cacheManagerName="qmplus"/>


    <!-- <bean id="cacheManagerControl" class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cacheManager-ref="ehcache" /> 

    <bean id="ehcacheControl"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:configLocation="classpath:/ehcache_control.xml" p:shared="true" /> -->


    <bean id="abstractEntityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        abstract="true">
        <!-- <property name="dataSource" ref="dataSource" /> -->
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <!-- <property name="packagesToScan" value="com.qmplus.entity" /> -->

    </bean>
    <bean id="dataSource" class="com.qmplus.dao.multitenancy.CustomRoutingDataSource">
        <constructor-arg ref="defaultDataSource" name="defaultDataSource"></constructor-arg>
    </bean>

    <bean id="multiTenantConnectionProvider"
        class="com.qmplus.dao.multitenancy.MultiTenantConnectionProviderImpl" />

    <bean id="multiTenantIdentifierResolver"
        class="com.qmplus.dao.multitenancy.MultiTenantIdentifierResolver" />

    <!-- Multiple transaction managers for JPA EntityManagerFactories -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="controlTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="controlEntityManagerFactory" />
    </bean>

    <jpa:repositories base-package="com.qmplus.dao" factory-class="com.qmplus.dao.impl.RepositoryFactoryBean"
        entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" />
    <jpa:repositories base-package="com.qmplus.control.dao" factory-class="com.qmplus.control.dao.impl.ControlRepositoryFactoryBean"    
     entity-manager-factory-ref="controlEntityManagerFactory"   transaction-manager-ref="controlTransactionManager" />

</beans>


**ehcache.xml :-**
<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false" name="qmplus" dynamicConfig="true" monitoring="autodetect">
    <diskStore path="java.io.tmpdir" />
    <terracottaConfig url="localhost:9510" rejoin="true" /> 
    <defaultCache eternal="false" overflowToDisk="false"
        diskPersistent="false" maxElementsInMemory="10000"
        timeToIdleSeconds="300" copyOnRead="true" copyOnWrite="true"
        timeToLiveSeconds="600" />
    <cache name="org.hibernate.cache.UpdateTimestampsCache" maxEntriesLocalHeap="5000" eternal="true">
            <persistence strategy="localTempSwap"/>
    </cache>

    <cache name="org.hibernate.cache.StandardQueryCache" 
        maxElementsInMemory="500" eternal="false" timeToLiveSeconds="120" maxEntriesLocalHeap="5">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

【问题讨论】:

    标签: spring ehcache terracotta


    【解决方案1】:

    虽然您的 CacheManager 具有 terracottaConfig 元素,但没有一个缓存具有使它们集群化所需的 terracotta 元素。

    查看 BigMemoryMax 4.2 中distributed configuration 的文档。

    • 对于 4.1 之前的 Terracotta 版本,只有当至少有一个集群缓存处于活动状态时,集群 CacheManager 才会连接到 Terracotta 集群。鉴于您的ehcache.xml 中的问题,情况并非如此。
    • 对于 Terracotta 4.1 及更高版本,集群 CacheManager 将急切地连接到 Terracotta 集群。

    【讨论】:

    • 谢谢路易斯,但我的问题有些不同,当我们一边启动兵马俑服务器而另一边在 tomcat 服务器上启动应用程序时,它没有连接到兵马俑服务器并且似乎在兵马俑连接的客户端中( 0)。那我怎么连接呢?
    • 查看已编辑的答案 - 我认为您没有其他问题,问题是 cache 元素中缺少 terracotta 元素。
    猜你喜欢
    • 2013-03-31
    • 2023-03-21
    • 2010-10-30
    • 2020-12-19
    • 1970-01-01
    • 2010-12-22
    • 2019-10-13
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多