persistence.xml不同的事务types属性
在persistence.xml JPAconfiguration文件中,你可以有一行:
<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”什么区别?
我还注意到一些事务types丢失的persistence.xml文件。 这是对的吗?
默认
在JavaEE环境中缺省为JTA ,在JavaSE环境中缺省为RESOURCE_LOCAL 。
RESOURCE_LOCAL
使用<persistence-unit transaction-type="RESOURCE_LOCAL">您负责EntityManager ( PersistenceContext/Cache )的创build和跟踪
- 您必须使用
EntityManagerFactory来获取一个EntityManager - 得到的
EntityManager实例是一个PersistenceContext/Cache一个EntityManagerFactory可以仅通过@PersistenceUnit注解注入(而不是@PersistenceContext) - 您不允许使用
@PersistenceContext来引用RESOURCE_LOCALtypes的单位 - 您必须使用
EntityTransactionAPI来开始/提交对EntityManger每个调用 - 调用
entityManagerFactory.createEntityManager()两次会产生两个单独的EntityManager实例,因此有两个独立的PersistenceContexts/Caches。 - 使用
EntityManager多个实例几乎不是一个好主意(除非已经销毁了第一个实例,否则不要创build第二个实例)
JTA
使用<persistence-unit transaction-type="JTA">容器将执行EntityManager ( PersistenceContext/Cache )的创build和跟踪。
- 您不能使用
EntityManagerFactory来获取EntityManager - 你只能得到一个由容器提供的
EntityManager - 一个
EntityManager只能通过@PersistenceContext注解注入(而不是@PersistenceUnit) - 您不允许使用
@PersistenceUnit来引用JTAtypes的单元 - 容器给出的
EntityManager是对与JTA事务关联的PersistenceContext/Cache的引用。 - 如果没有正在进行的JTA事务,则不能使用
EntityManager因为没有PersistenceContext/Cache。 - 每个在同一个事务中使用
EntityManager引用同一个单元的人都会自动引用同一个PersistenceContext/Cache -
PersistenceContext/Cache在JTA提交时被刷新并清除