0

I have an EntityManager and a JobDAO class which has many methods which use the EntityManager to select/update/delete/insert.

For some methods, I now am getting a javax.persistence.TransactionRequiredException: Executing an update/delete query .. about not having a transaction.

I have a @Transactional annotation on the method calling the other methods.

I have now fixed it somewhat by using my own database connection for some of these commands, but I'd like to find the SQL that causes the problem.

One idea I have is to add a transaction checker method call to the end of each of the 20 methods that are suspicious. But I'd like to know whether you have a better idea to check on the EntityManager, for example by logging all SQL so I can find the last SQL where it stopped working.

Adder
  • 5,708
  • 1
  • 28
  • 56
  • There is a configuration property for the EntityManagerFactory that will cause your ORM provider (Hibernate?) to log all sql. See: https://www.baeldung.com/sql-logging-spring-boot – StvnBrkdll Jul 19 '19 at 12:51

1 Answers1

1

See this post on SO for configuring the ORM provider (e.g. Hibernate, etc.) to log sql: How to view the SQL queries issued by JPA?

In addition, you can programatically ask the EntityManager if it is currently in a transaction, see isJoinedToTransaction().

Also..., make sure that the class with the methods that have the @Transactional annotation is a Spring Bean, otherwise the annotation does nothing.

In addition you have to tell Spring to enable transaction management. This post will help you understand how to do that: https://www.baeldung.com/transaction-configuration-with-jpa-and-spring

StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31
  • I don't have an `application.properties` file. Where would I place it? – Adder Jul 19 '19 at 12:58
  • This post on Baeldung will help you understand how to configure your jpa provider: https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa – StvnBrkdll Jul 19 '19 at 13:03