如何使用ehcache.xml和terracotta为多个EntityManager配置缓存

时间:2014-11-25 04:48:45

标签: spring ehcache terracotta

配置: - 使用兵马俑提供多个实体管理器和单个CacheManager的配置。但是当我们启动赤土陶器服务器显示连接客户端(0)时,我们无法看到使用赤陶的缓存。因此,如果在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>

1 个答案:

答案 0 :(得分:0)

虽然CacheManager具有terracottaConfig元素,但没有一个缓存具有必需的terracotta元素,以使其成为群集。

查看BigMemoryMax 4.2中的distributed configuration文档。

  • 对于4.1之前的Terracotta版本,只有在至少有一个群集Cache实时时,群集CacheManager才会连接到Terracotta群集。鉴于ehcache.xml
  • 中存在此问题,情况并非如此
  • 对于Terracotta 4.1及以上版本,集群CacheManager将热切地连接到Terracotta集群。