Search code examples
spring-boothibernatejta

Is JTA recommended with Hibernate in Springboot


I have to use hibernate along with spring boot. Is it recommended to use JTA transaction manager in this stack?

If JTA is recommended, how to access current session programmatically in controller or service layer ? with example will be more helpful.


Solution

  • As spring documentation says:

    Typically, you need an application server’s JTA capability only if your application needs to handle transactions across multiple resources, which is not a requirement for many applications.

    And according to spring boot documentation:

    Spring Boot supports distributed JTA transactions across multiple XA resources by using either an Atomikos or Bitronix embedded transaction manager. JTA transactions are also supported when deploying to a suitable Java EE Application Server.

    When a JTA environment is detected, Spring’s JtaTransactionManager is used to manage transactions. Auto-configured JMS, DataSource, and JPA beans are upgraded to support XA transactions. You can use standard Spring idioms, such as @Transactional, to participate in a distributed transaction.

    As for your second question you can have a look at this answer.