简体   繁体   中英

Spring Hibernate JPA - No JTA TransactionSynchronizationRegistry

I'm setting up hibernate 5.4.13 with spring 5.2.5 to use JPA. It's not working yet with the error "javax.persistence.TransactionRequiredException: no transaction is in progress".

My debug printed the below out. Googling this it would appear it's a callback class and you only need one of these if you wish to write custom hooks.

I'm just wondering if this is the source of my problem. Can anyone confirm this is not required?

DEBUG jta.JtaTransactionManager       - No JTA TransactionSynchronizationRegistry found at default JNDI location [java:comp/TransactionSynchronizationRegistry]
javax.naming.NameNotFoundException: Name [TransactionSynchronizationRegistry] is not bound in this Context. Unable to find [TransactionSynchronizationRegistry].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:817)

Suggestions:

  1. you can specify alternative JNDI location for transaction synchronization registry via system properties:

     -Djbpm.tsr.jndi.lookup=java:comp/env/TransactionSynchronizationRegistry
  2. set up in the Tomcat <tomcat-home>/conf/context.xml . file:

     <Resource auth="Container" name="comp/env/TransactionSynchronizationRegistry" type="javax.transaction.TransactionSynchronizationRegistry" factory="bitronix.tm.BitronixTransactionSynchronizationRegistryObjectFactory" /> <ResourceLink global="comp/env/TransactionSynchronizationRegistry" name="comp/env/TransactionSynchronizationRegistry" />

    we need to add a connection pool to showcase the usage of JTA later on:

     <ResourceLink global="jdbc/myDB" name="jdbc/myDB" type="javax.sql.DataSource"/> <Resource driverClassName="org.h2.Driver" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" name="jdbc/myDB" password="" type="javax.sql.DataSource" url="jdbc:h2:tcp://localhost/~/test" username="sa"/>

    Now we need to add the actual JTA-related resources, starting with the the TransactionSynchronizationRegistry. TransactionSynchronizationRegistryFactory will end up at java:comp/env/TransactionSynchronizationRegistry and not at java:comp/TransactionSynchronizationRegistry as the JEE-spec requires.

    A little example on how to use JTA in your webapplication via spring:

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="false"/> </bean> </property> <propertyname="jpaProperties"> <props> <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop> </props> </property> </bean> <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myDB" resource-ref="true"/> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionSynchronizationRegistryName" value="java:comp/env/TransactionSynchronizationRegistry"/> <property name="transactionManagerName" value="java:comp/UserTransaction"/>

More details here: https://codepitbull.wordpress.com/2011/07/08/tomcat-7-with-full-jta/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM