Search code examples
javaspringhibernatetransactionsjta

Can I use Hibernate with JTA?


If JTA is an API, can I use Hibernate as an implementation of JTA?

I have an application with Spring and Hibernate and I wonder which framework should be responsible for transactions, Spring or Hibernate?


Solution

  • Hibernate is not an implementation of JTA. Hibernate is a JPA implementation.

    JTA is an enterprise transaction spec, that's implemented by Java EE providers or stand-along transaction managers (e.g. Bitronix).

    Hibernate offers a Transaction API abstraction because ORM tools employ a transactional write-behind Persistence Context.

    Spring offers a transaction management abstraction, which allows you to switch from RESOURCE_LOCAL to JTA transactions with just some trivial configuration changes.

    Spring manages to integrate on top of Hibernate/JPA Transaction API abstraction too.

    If you use Spring, then you should take advantage of its transaction management abstraction, and so you don't have to use the Hibernate/JPA Transaction API.

    Because Spring uses AOP, the transaction management is decoupled from your business logic, which would not be the case if you were using the programmatic Hibernate/JPA Transaction API.