Search code examples
springhibernatejtainfinispan

spring + hibernate + infinispan as 2nd level cache


I tried to setup infinispan as 2nd level cache for hibernate in spring+tomcat based app.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${hibernate.connection.driver_class}"/>
    <property name="url" value="${hibernate.connection.url}"/>
    <property name="username" value="${hibernate.connection.username}"/>
    <property name="password" value="${hibernate.connection.password}"/>
    <property name="maxActive" value="${hibernate.connection.maxActive}"/>
    <property name="maxIdle" value="${hibernate.connection.maxIdle}"/>
    <property name="minIdle" value="${hibernate.connection.minIdle}"/>
    <property name="maxWait" value="${hibernate.connection.maxWait}"/>
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
        <bean class="com.arjuna.ats.jta.TransactionManager" factory-method="transactionManager"/>
    </property>
    <property name="userTransaction">
        <bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.example"/>

    <!---->
    <property name="hibernateProperties" ref="db-properties"/>
</bean>

And the properties are:

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.infinispan.InfinispanRegionFactory

I get an exception:

Caused by: org.infinispan.CacheException: This is transactional cache but no transaction manager could be found. Configure the transaction manager lookup properly.

How do I configure a transaction manager lookup?


Solution

  • Both Hibernate and Infinispan need to know about the TransactionManager. If you tell Hibernate about the TransactionManager, it will in turn tell Infinispan for you. I am guessing Spring has a way to tell Hibernate about the JTA setup, but I could not find it. Hibernate for its part (I see you are at least trying to use Hibernate 4) needs to be told about which org.hibernate.service.jta.platform.spi.JtaPlatform to use. org.hibernate.service.jta.platform.spi.JtaPlatform is the contract for Hibernate to know how to interact with the JTA environment.