问题描述
在persistence.xml JPA配置文件中,您可以使用以下行:
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">
或有时:
<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
我的问题是:
transaction-type =JTA
和 transaction-type =RESOURCE_LOCAL
?
我还注意到一些缺少事务类型的persistence.xml文件。这是正确的吗?
I also noticed some persistence.xml files with the transaction-type missing. Is it correct?
推荐答案
默认值
默认为 JavaEE环境中的JTA 和JavaSE环境中的 RESOURCE_LOCAL 。
Defaults
Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment.
使用< persistence-unit transaction-type =RESOURCE_LOCAL>
您负责 EntityManager
( PersistenceContext / Cache
)创建和跟踪
With <persistence-unit transaction-type="RESOURCE_LOCAL">
you are responsible for EntityManager
(PersistenceContext/Cache
) creating and tracking
- 您必须使用
EntityManagerFactory
获取EntityManager
- 生成的
EntityManager
实例是PersistenceContext / Cache
可以通过 EntityManagerFactory code> @PersistenceUnit 仅限注释(不是@PersistenceContext
) - 您不被允许使用
@PersistenceContext
来引用类型的单位RESOURCE_LOCAL
- 你必须使用
EntityTransaction
用于开始/提交每次调用EntityManger的API
- 调用
entityManagerFactory.createEntityManager()
两次导致两个单独的EntityManager
实例,因此有两个单独的PersistenceContexts / Caches
。 - 使用多个
EntityManager
的实例几乎绝不是一个好主意(不要创建)第二个,除非你已经销毁了第一个)
- 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)
使用< persistence-unit transaction-type =JTA>
容器将执行 EntityManager
( PersistenceContext / Cache
)创建和跟踪。
With <persistence-unit transaction-type="JTA">
the container will do EntityManager
(PersistenceContext/Cache
) creating and tracking.
- 你不能使用
EntityManagerFactory
获取EntityManager
- 您只能获得
EntityManager
由容器提供 - 可以通过<$ c注入
EntityManager
$ c> @PersistenceContext 仅注释(不是@PersistenceUnit
) - 您不能使用
@PersistenceUnit
引用JTA类型的单位 -
EntityManager
由容器是对与JTA事务关联的PersistenceContext / Cache
的引用。 - 如果没有正在进行的JTA事务,
EntityManager
无法使用,因为没有PersistenceContext / Cache
。 - 每个人都有
EntityManager
对同一事务中同一单元的引用将自动引用相同的PersistenceContext / Cache
-
PersistenceContext / Cache
在JTA提交时刷新并清除
- 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
这篇关于persistence.xml不同的事务类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!