persistence.xml 不同的事务类型属性 [英] persistence.xml different transaction-type attributes
问题描述
在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?
推荐答案
Defaults
在 JavaEE 环境中默认为 JTA,在 JavaSE 环境中默认为 RESOURCE_LOCAL.
Defaults
Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment.
使用
你负责 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
只能通过@PersistenceUnit
注释注入(不是@PersistenceContext
) - 不允许使用
@PersistenceContext
来引用类型为RESOURCE_LOCAL
的单元 - 您必须使用
EntityTransaction
API 来开始/提交对您的EntityManger
的每次调用 - 调用
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)
使用
容器将做 EntityManager
(PersistenceContext/Cache
) 创建和跟踪.
With <persistence-unit transaction-type="JTA">
the container will do EntityManager
(PersistenceContext/Cache
) creating and tracking.
- 您不能使用
EntityManagerFactory
来获取EntityManager
- 你只能得到一个容器提供的
EntityManager
EntityManager
只能通过@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 不同的事务类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!