persistence.xml不同的事务类型属性

Ger*_*llo 68 java jpa jta persistence.xml java-ee

在persistence.xml JPA配置文件中,您可以使用如下行:

<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type="JTA">
Run Code Online (Sandbox Code Playgroud)

或有时:

<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type=”RESOURCE_LOCAL”>
Run Code Online (Sandbox Code Playgroud)

我的问题是:

transaction-type="JTA"和之间有什么区别transaction-type=”RESOURCE_LOCAL”

我还注意到一些缺少事务类型的persistence.xml文件.这是对的吗?

Jir*_*ser 113

默认

默认为JavaEE环境中的JTA和JavaSE环境中的RESOURCE_LOCAL.

RESOURCE_LOCAL

随着<persistence-unit transaction-type="RESOURCE_LOCAL">你负责EntityManager(PersistenceContext/Cache)创建和跟踪

  • 你必须用它EntityManagerFactory来得到一个EntityManager
  • 结果EntityManager实例是一个PersistenceContext/Cache An EntityManagerFactory只能通过@PersistenceUnit注释注入(不是@PersistenceContext)
  • 您不能@PersistenceContext用于引用类型的单位RESOURCE_LOCAL
  • 您必须使用EntityTransactionAPI开始/提交每次调用您的EntityManger
  • 调用entityManagerFactory.createEntityManager()两次导致两个单独的EntityManager实例,因此两个单独的实例PersistenceContexts/Caches.
  • 拥有多个EntityManager正在使用的实例几乎绝不是一个好主意(除非你已经破坏了第一个实例,否则不要创建第二个实例)

JTA

随着<persistence-unit transaction-type="JTA">容器将do EntityManager(PersistenceContext/Cache)创建和跟踪.

  • 你不能用它EntityManagerFactory来得到一个EntityManager
  • 您只能获得EntityManager容器提供的产品
  • 一个EntityManager可以通过被注入@PersistenceContext注解只有(未@PersistenceUnit)
  • 您不能@PersistenceUnit用于引用JTA类型的单位
  • EntityManager由给定的容器是将参考PersistenceContext/Cache与JTA事务相关联.
  • 如果没有正在进行的JTA事务,EntityManager则不能使用,因为没有PersistenceContext/Cache.
  • EntityManager在同一事务中引用同一单元的每个人都将自动引用相同的单元PersistenceContext/Cache
  • PersistenceContext/Cache刷新和JTA清除提交时间

  • "使用一个以上的EntityManager实例几乎绝不是一个好主意" - 这是你的意见吗?通常需要在并发应用程序中打开多个打开的EntityManagers.一般来说,一个非常好的答案. (3认同)
  • 您可以通过附加信息和简短示例来查看答案的来源:http://tomee.apache.org/jpa-concepts.html (3认同)
  • 我不同意声明"你不允许使用@PersistenceUnit来引用JTA类型的单位".我认为您可以轻松地执行此操作,并且可以使用方法emf.createEntityManager()来获取实体管理器.您可以使用SynchronizationType类型的参数来定义当前事务是否应该立即加入,或者您在调用em.joinTransaction()方法时自己加入它 (2认同)