103

I have queries as below:

  1. What is the difference of these two?
  2. Are both of these supported by all databases?
  3. Are JPA TransactionManager and JTA TransactionManager different?
informatik01
  • 16,038
  • 10
  • 74
  • 104
cometta
  • 35,071
  • 77
  • 215
  • 324

4 Answers4

108

JPA implementations have the choice of managing transactions themselves (RESOURCE_LOCAL), or having them managed by the application server's JTA implementation.

In most cases, RESOURCE_LOCAL is fine. This would use basic JDBC-level transactions. The downside is that the transaction is local to the JPA persistence unit, so if you want a transaction that spans multiple persistence units (or other databases), then RESOURCE_LOCAL may not be good enough.

JTA is also used for managing transactions across systems like JMS and JCA, but that's fairly exotic usage for most of us.

To use JTA, you need support for it in your application server, and also support from the JDBC driver.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 3
    it seems glassfih does not allow me t use resource_local - how can I do that? – Pete_ch Dec 16 '12 at 18:08
  • 3
    _As a side note:_ one still can get JTA functionality, even without a full Java EE application server by using third party solutions, like for example [Atomikos](https://www.atomikos.com/). So you can have a lightweight web container like Tomcat and still get the JTA support. – informatik01 Sep 13 '16 at 11:15
98

As an addition to other answers

Here is an excerpt from the extremely useful article (published on the Apache TomEE website), which can also help answer the OP's first question (the link to the article is below).


Comparing RESOURCE_LOCAL and JTA persistence contexts


With <persistence-unit transaction-type="RESOURCE_LOCAL"> YOU are responsible for EntityManager (PersistenceContext/Cache) creating and tracking...

  • You must use the EntityManagerFactory to get an EntityManager
  • The resulting EntityManager instance is a PersistenceContext/Cache
  • An EntityManagerFactory can be injected via the @PersistenceUnit annotation only (not @PersistenceContext)
  • You are not allowed to use @PersistenceContext to refer to a unit of type RESOURCE_LOCAL
  • You must use the EntityTransaction API to begin/commit around every call to your EntityManger
  • Calling entityManagerFactory.createEntityManager() twice results in two separate EntityManager instances and therefor two separate PersistenceContexts/Caches.
  • It is almost never a good idea to have more than one instance of an EntityManager in use (don't create a second one unless you've destroyed the first)


With <persistence-unit transaction-type="JTA"> the CONTAINER will do EntityManager (PersistenceContext/Cache) creating and tracking...

  • You cannot use the EntityManagerFactory to get an EntityManager
  • You can only get an EntityManager supplied by the container
  • An EntityManager can be injected via the @PersistenceContext annotation only (not @PersistenceUnit)
  • You are not allowed to use @PersistenceUnit to refer to a unit of type JTA
  • The EntityManager given by the container is a reference to the PersistenceContext/Cache associated with a JTA Transaction.
  • If no JTA transaction is in progress, the EntityManager cannot be used because there is no PersistenceContext/Cache.
  • Everyone with an EntityManager reference to the same unit in the same transaction will automatically have a reference to the same PersistenceContext/Cache
  • The PersistenceContext/Cache is flushed and cleared at JTA commit time

Anyone interested in learning the Java Persistence API - please do yourself a favor and read the full article here: JPA Concepts: JPA 101.

informatik01
  • 16,038
  • 10
  • 74
  • 104
  • 11
    Just wanted to add one point: If you are using Spring you can use @ PersistenceContext and EntityManager with Resource_Local. In this case the Spring container can manage the transaction using @ Transactional annotation. – Sam Jun 23 '15 at 12:09
  • In my project `transaction-type=RESOURCE_LOCAL` and `@PersistenceContext` and `@Transactional` managed by Spring – Ravi Parekh Mar 01 '16 at 16:22
  • I am hitting the triangle facing up because of the link you provided. – Koray Tugay May 23 '17 at 18:08
  • @KorayTugay I am sorry, didn't quite understand what you say, what triangle? – informatik01 May 23 '17 at 19:13
  • @informatik01 upvote button. (i can not just comment plus 1, not allowed!) – Koray Tugay May 23 '17 at 19:14
  • @KorayTugay Oh, it's OK. Hope you found useful information here ) – informatik01 May 23 '17 at 19:16
  • @informatik01 Ok... I think there are common rules with JTA and I want to clarify on think. Is correct that **flush** is sending data to DB if is triggered? In my case I trigger **flush** some changes are propagated to DB and also exception is occurring; after I trigger **rollback** (using setRollbackOnly) but the propagated changes are still in DB. – Radu Linu Oct 05 '17 at 19:22
  • 1
    @LinuRadu This answer might be helpful for you: [What's the use of session.flush() in Hibernate](https://stackoverflow.com/a/14322619/814702) – informatik01 Oct 05 '17 at 19:31
  • @informatik01 Thanks. Is very interesting because on my flush action the changes are sent to DB and could ne be reverted – Radu Linu Oct 05 '17 at 20:03
19

Resource_Local and JTA are transaction managers (methods of doing transactions). This is not the property of database but the component responsible for coordinating transactions. JPA and JTA transaction managers are different. JPA transaction manager is responsible for JPA transactions and you want to use one if you are only doing JPA transaction. JTA transaction manager is general purpose transaction manager and can enlist other resources such as JMS queues in transaction. Typically Java EE containers employ a JTA transaction manager for EJBs, JPA entities, etc.

Chandra Patni
  • 17,347
  • 10
  • 55
  • 65
1

resource_local vs JTA its about local transaction vs global transaction. It's about can we manage multiple resources under a single transaction.

CMT vs BMT its about who is opening and closing transaction - application developer or application server.

Pushpendra
  • 143
  • 1
  • 7