在 persistence.xml JPA 配置文件中,您可以有如下行:
<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"
and transaction-type=”RESOURCE_LOCAL”
?
我还注意到一些 persistence.xml 文件缺少事务类型。这是对的吗?
Defaults
默认为JTA在 JavaEE 环境中并RESOURCE_LOCAL在 JavaSE 环境中。
RESOURCE_LOCAL
With <persistence-unit transaction-type="RESOURCE_LOCAL">
你负责EntityManager
(PersistenceContext/Cache
) 创建和跟踪
- 您必须使用
EntityManagerFactory
得到一个EntityManager
- 所结果的
EntityManager
实例是一个PersistenceContext/Cache
An EntityManagerFactory
可以通过注射@PersistenceUnit
仅注释(不@PersistenceContext
)
- 您无权使用
@PersistenceContext
引用类型单位RESOURCE_LOCAL
- 您必须使用
EntityTransaction
用于开始/提交每次调用的 APIEntityManger
- Calling
entityManagerFactory.createEntityManager()
两次导致两个不同的结果EntityManager
实例,因此有两个单独的PersistenceContexts/Caches
.
- 拥有多个实例几乎从来都不是一个好主意
EntityManager
正在使用(除非您已销毁第一个,否则不要创建第二个)
JTA
With <persistence-unit transaction-type="JTA">
容器就可以了EntityManager
(PersistenceContext/Cache
)创建和跟踪。
- 您不能使用
EntityManagerFactory
得到一个EntityManager
- 你只能得到一个
EntityManager
由容器提供
- An
EntityManager
可以通过注射@PersistenceContext
仅注释(不@PersistenceUnit
)
- 您无权使用
@PersistenceUnit
引用 JTA 类型的单元
- The
EntityManager
容器给出的是对PersistenceContext/Cache
与 JTA 事务关联。
- 如果没有 JTA 事务正在进行,则
EntityManager
无法使用,因为没有PersistenceContext/Cache
.
- 每个人都有一个
EntityManager
在同一交易中引用同一单位将自动具有对同一单位的引用PersistenceContext/Cache
- The
PersistenceContext/Cache
在 JTA 提交时刷新并清除
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)