Announcement Announcement Module
No announcement yet.
Spring, Hibernate, JPA, Infinispan and Tomcat, how to expose the TransactionManager Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, Hibernate, JPA, Infinispan and Tomcat, how to expose the TransactionManager


    I have the following setup. We are using Tomcat as the container. JPA is used to manage our entities, Hibernate is the JPA provider. We also use Infinispan as the L2C. On top of this everything is running within Spring.

    We have a spring-hibernate.xml file that is defined as:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	<!-- use annotations for transaction management -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    	<!-- Process the PersistenceUnit and PersistenceContext annotations -->
     	<bean class=""/>
    	<!-- spring's simple connection data source -->
    	<bean id="dataSource.hsqldb" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true" scope="singleton">
    		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    		<property name="url" value="${datasource.url}" />
    		<property name="username" value="" />
    		<property name="password" value="" />
    	<!-- application datasource -->
    	<bean id="dataSource.jndi" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
        	<property name="jndiName" value="java:comp/env/jdbc/ds" />
    	<!-- official dataSource -->
    	<alias alias="dataSource" name="${}" />
    	<!-- Transaction Manager -->
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
            <property name="dataSource" ref="dataSource" />
    	<!-- Transaction Template -->
    	<bean id="" class="" >
    		<property name="transactionManager" ref="transactionManager"/>
        <!-- Hibernate/JPA configuration -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="jpa" />
            <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
            <property name="dataSource" ref="dataSource" />
            <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="false" />
                    <property name="generateDdl" value="true" /> 
                    <property name="databasePlatform" value="${hibernate.dialect}" />
    We also have the following persistence.xml file
    <?xml version="1.0" encoding="UTF-8"?>
        <persistence-unit name="jpa">
                <property name="hibernate.cache.infinispan.cfg" value="infinispan-config.xml"/>
                <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
                <property name="hibernate.cache.infinispan.cachemanager" value="java:CacheManager/entity" />
                <property name="hibernate.cache.infinispan.use_synchronization"  value="true"/>
                <property name="hibernate.cache.infinispan.statistics" value="true" />
                <property name="hibernate.cache.use_second_level_cache" value="true" />
                <property name="hibernate.cache.use_query_cache" value="true" />
                <property name="hibernate.generate_statistics" value="true" />
                <property name="hibernate.cache.use_structured_entries" value="true" />
    I need to configure Infinispan to use the same transaction manager as JPA (Hibernate). I am stumbling upon the fact that we have defined a jpa transaction but I can't see how I can retrieve the JTA from it. On the other side if I define the JTA transaction how do I tell it to use the entity manager?

    I understand that I could also specify a transaction manager lookup class in the persistence xml such as:
    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
    But since I am using Tomcat I have to somehow wire it via JNDI according to this documentation:

    I am bit lost could anyone point me to the right direction?

    Thank you.

  • #2
    JPA != JTA... So if you use JpaTransactionManager you aren't using JTA and also cannot use JTA. You have to configure the JtaTransactionManager JTA and LOCAL transactions work in a different manner and you don't tell the JtaTransactionManager which resources to manage, the resources to manage register themselves (hence the need for the explicit transaction.manager_lookup_class).

    Tomcat doesn't have native JTA and you will have to use another solution like Atomikos to enable JTA transactions. If you switch to, for instance, JBoss then you have a server which supports transactions.


    • #3

      I understand that the JpaTransactionManager is not the right option.

      I was under the impression I could configure JPA to use JTA by specifying the hibernate.transaction.manager_lookup_class property in the persistence.xml file, am I wrong?
      Since I am using JPA, how would I define an EntityManager that is associated with JTA?

      From your response I understand my only option is to define a JtaTransactionManager, and since I am using Tomcat, I have to rely on Spring org.springframework.transaction.jta.JtaTransaction Manager, but I have to configure it to use a third party, is that right?

      Thank you.