Question

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?

Was it helpful?

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.

OTHER TIPS

The easy answer for spring is to add (inside id="sessionFactory" spring XML configuration) :

<property name="jtaTransactionManager" ref="transactionManager"/>

This is available in spring 3.2.2.RELEASE (it maybe available in older spring versions too, I would guess since spring 3.1.x)

This should achieve what Steve Ebersole points at. This causes Spring to provide a JtaPlatform via the class https://github.com/SpringSource/spring-framework/blob/master/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java

See JavaDoc for class org.springframework.orm.hibernate4.LocalSessionFactoryBean (that you are using) concerning JTA usage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top