Persistence unit as RESOURCE_LOCAL or JTA?
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.
persistence.xml different transaction-type attributes
Defaults
Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment.
RESOURCE_LOCAL
With <persistence-unit transaction-type="RESOURCE_LOCAL">
you are responsible for EntityManager
(PersistenceContext/Cache
) creating and tracking
- You must use the
EntityManagerFactory
to get anEntityManager
- The resulting
EntityManager
instance is aPersistenceContext/Cache
AnEntityManagerFactory
can be injected via the@PersistenceUnit
annotation only (not@PersistenceContext
) - You are not allowed to use
@PersistenceContext
to refer to a unit of typeRESOURCE_LOCAL
- You must use the
EntityTransaction
API to begin/commit around every call to yourEntityManger
- Calling
entityManagerFactory.createEntityManager()
twice results in two separateEntityManager
instances and therefor two separatePersistenceContexts/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)
JTA
With <persistence-unit transaction-type="JTA">
the container will do EntityManager
(PersistenceContext/Cache
) creating and tracking.
- You cannot use the
EntityManagerFactory
to get anEntityManager
- 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 thePersistenceContext/Cache
associated with a JTA Transaction. - If no JTA transaction is in progress, the
EntityManager
cannot be used because there is noPersistenceContext/Cache
. - Everyone with an
EntityManager
reference to the same unit in the same transaction will automatically have a reference to the samePersistenceContext/Cache
- The
PersistenceContext/Cache
is flushed and cleared at JTA commit time
ResourceLocal to JTA
I finally was able to fix my problem. From my searches it resulted that you can not use EntityManager when you are using JTA within ManagedBeans for example. However it can be used in a stateless bean and then we can inject this Stateless Bean to the ManagedBean and use its methods. The procedure is as follows:
create an EJB (a simple class with @Stateless annotation)
move the method that uses EntityManager to the EJB
- inject the EJB into your managed bean (using @EJB annotation) and call the relevant method
For more information refer to this other post: JTA & MySQL
Cannot inject RESOURCE_LOCAL container managed EntityManager using @PersistenceContext
JTA : In Java EE environment, transactions are managed by the container & by default its JTA
transaction. You can get entity manager by lookup or injection.
RESOURCE_LOCAL : In Java SE, application have to manage transactions explicitly & resource local transactions are native transactions. You have to create EntityManagerFactory
& then can create entity manager from it.
As you are deploying it in application server, change the transaction-type
to JTA
in persistence.xml.
What properties does persistence-unit have in apache openjpa?
This is reference for persistence properties for JPA 2.X:
https://www.developer.com/java/ent/standard-persistence-properties-in-jpa-2.html
This is reference for openjpa: https://openjpa.apache.org/builds/3.0.0/apache-openjpa/docs/ref_guide_conf_openjpa.html
And here are also properties for Hibernate and EclipseLink: Properties reference for hibernate in persistence.xml
Related Topics
Case Insensitive String as Hashmap Key
Format File Size as Mb, Gb, etc
"Loading Class Com.Mysql.Jdbc.Driver ... Is Deprecated" Message
Where Is the Documentation for the Values() Method of Enum
Jfreechart Series Tool Tip Above Shape Annotation
Named Placeholders in String Formatting
Mockito:How to Verify Method Was Called on an Object Created Within a Method
Struts 2:There Is No Action Mapped for Namespace [/]
How to Call a Method with a Separate Thread in Java
Differencebetween Class.This and This in Java
Mockito: Trying to Spy on Method Is Calling the Original Method
How to Get the File Name from a String Containing the Absolute File Path
Maven: Failed to Read Artifact Descriptor