Can I use Hibernate with JTA?
Asked Answered
C

1

10

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?

Chesterfield answered 31/1, 2015 at 16:27 Comment(5)
Spring annotations should be your choice. Choose the transaction manager that makes sense for your deployment.Boswell
Hibernate doesn't handle transactions, a transaction manager does. You set a transaction manager, and calling the method on Session simply delegates to this transaction manager. Same with Spring. The actual transaction manager used is usually just the datasource's transaction manager - which sets up transactions on the JDBC connections. A more generic approach is JTA.Sauerkraut
Transaction Manager is part of JTA (something like enetityManager in JPA )?Chesterfield
No, the transaction manager is an adapter pattern - it adapts various transaction frameworks to the framework at hand. JTA is one such framework. JDBC also has transactions.Sauerkraut
Ok but can I use hibenrate like a implementation of JTA (sth like JPA with hibernate)?Chesterfield
C
23

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.

Cowherd answered 1/2, 2015 at 7:5 Comment(2)
Ok, thanks for comprehensive answer. Can you explain me what is RESOURCE_LOCAL ?Chesterfield
It is the JDBC Connection transaction support. The JDBC Connection has the commit/ rollback methods for controlling one database transaction. The RESOURCE_LOCAL can only use one DataSource per transaction, as opposed to JTA which can distribute a global transaction over multiple DataSources.Cowherd

© 2022 - 2024 — McMap. All rights reserved.