How to configure Caching for Multiple EntityManagers with ehcache.xml and terracotta

1.6k views Asked by At

Configuration:-Given configuration of Multiple Entity Managers and Single CacheManager with terracotta. But when we start terracotta server showing Connected Clients(0) so we are not able to see caching using terracotta. So Please check this configuration if found any issue in ehcache.xml file pls let me.

<?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

There are 1 answers

2
Louis Jacomet On

While your CacheManager has the terracottaConfig element, none of the caches have the required terracotta element to make them clustered.

Have a look at the documentation for distributed configuration in BigMemoryMax 4.2.

  • For Terracotta version before 4.1, a clustered CacheManager will only connect to the Terracotta cluster once there is at least one clustered Cache live. Which is not the case given the issue in your ehcache.xml.
  • For Terracotta version 4.1 and above, a clustered CacheManager will eagerly connect to the Terracotta cluster.