Spring IllegalStateException: A JTA EntityManager cannot use getTransaction()

后端 未结 2 658
Happy的楠姐
Happy的楠姐 2021-01-20 07:56

So after a big refactoring project, I am left with this exception and am unsure as how to correct it. It\'s dealing with some code that I did not write and I am unfamiliar w

相关标签:
2条回答
  • 2021-01-20 08:04

    Use WebSphereTransactionManagerLookup for the transaction manager lookup in Hibernate

    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereTransactionManagerLookup" />
    

    and remove your current transaction manager and replace it with the WebSphereUowTransactionManager.

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
    

    for your transaction manager lookup in Spring.

    See IBM Websphere and Spring docs for more in depth documentation.

    0 讨论(0)
  • 2021-01-20 08:26

    The problem is your configuration. You have hibernate configured for JTA.

    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
    

    Whereas you are using local transactions instead of distributed transactions.

    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)
    

    You have 2 possible solutions

    1. remove the JpaTransactionManager and replace it with a JTA transaction manager
    2. remove the remove the hibernate.transaction.manager_lookup_class from the hibernate settings.

    If you don't really need distributed transactions option 2 is the easiest, if you need distributed transactions simply adding <tx:jta-transaction-manager /> will setup a proper JTA tx manager for your environment. Remove the definition for the JpaTransactionManager.

    Update:

    Your configuration is flawed in 2 ways.

    1. Your EntityManager configuration already contains a jndi lookup for the datasource, which you override in your applicationContext by configuring a local datasource
    2. You have both a <tx:jta-transaction-manager /> and JpaTransactionManager which one do you want to use? At the moment the latter is overriding the first one.

    Create 2 seperate configurations one for local testing and one for production using JTA en JNDI lookups. (Preferable your testing code only overrides the beans necessary).

    0 讨论(0)
提交回复
热议问题