Spring IllegalStateException: A JTA EntityManager cannot use getTransaction()

后端 未结 2 996
旧时难觅i
旧时难觅i 2021-01-20 07:35

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:19

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

    
    

    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 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 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).

提交回复
热议问题