9

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?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
user3528733
  • 1,259
  • 7
  • 20
  • 37
  • 1
    Spring annotations should be your choice. Choose the transaction manager that makes sense for your deployment. – duffymo Jan 31 '15 at 16:29
  • 1
    Hibernate doesn't handle transactions, a transaction manager does. You set a [transaction manager](https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/transaction/TransactionFactory.html), and calling the method on `Session` simply delegates to this transaction manager. Same with [Spring](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/PlatformTransactionManager.html). 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. – Boris the Spider Jan 31 '15 at 16:33
  • Transaction Manager is part of JTA (something like enetityManager in JPA )? – user3528733 Jan 31 '15 at 16:37
  • 1
    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](http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html). – Boris the Spider Jan 31 '15 at 16:38
  • Ok but can I use hibenrate like a implementation of JTA (sth like JPA with hibernate)? – user3528733 Jan 31 '15 at 16:50

1 Answers1

22

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.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Ok, thanks for comprehensive answer. Can you explain me what is RESOURCE_LOCAL ? – user3528733 Feb 01 '15 at 12:06
  • 5
    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. – Vlad Mihalcea Feb 01 '15 at 12:35