In the persistence.xml JPA configuration file, you can have a line like:
<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type="JTA"> or sometimes:
<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type=”RESOURCE_LOCAL”> My question is:
What is the difference between transaction-type="JTA" and transaction-type=”RESOURCE_LOCAL” ?
I also noticed some persistence.xml files with the transaction-type missing. Is it correct?
"The transaction-type attribute is used to specify whether the entity managers provided by the entity manager factory for the persistence unit must be JTA entity managers or resource-local entity managers. The value of this element is JTA or RESOURCE_LOCAL.
The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database.
The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.
Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment.
With <persistence-unit transaction-type="RESOURCE_LOCAL"> you are responsible for EntityManager (PersistenceContext/Cache) creating and tracking
EntityManagerFactory to get an EntityManager EntityManager instance is a PersistenceContext/Cache An EntityManagerFactory can be injected via the @PersistenceUnit annotation only (not @PersistenceContext)@PersistenceContext to refer to a unit of type RESOURCE_LOCAL EntityTransaction API to begin/commit around every call to your EntityManger entityManagerFactory.createEntityManager() twice results in two separate EntityManager instances and therefor two separate PersistenceContexts/Caches.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.
EntityManagerFactory to get an EntityManager EntityManager supplied by the containerEntityManager can be injected via the @PersistenceContext annotation only (not @PersistenceUnit)@PersistenceUnit to refer to a unit of type JTAEntityManager given by the container is a reference to the PersistenceContext/Cache associated with a JTA Transaction.EntityManager cannot be used because there is no PersistenceContext/Cache.EntityManager reference to the same unit in the same transaction will automatically have a reference to the same PersistenceContext/Cache PersistenceContext/Cache is flushed and cleared at JTA commit timeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With