简体   繁体   English

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

[英]persistence.xml different transaction-type attributes

In the persistence.xml JPA configuration file, you can have a line like:在persistence.xml JPA 配置文件中,你可以有这样一行:

<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type="JTA">

or sometimes:或有时:

<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type=”RESOURCE_LOCAL”>

My question is:我的问题是:

What is the difference between transaction-type="JTA" and transaction-type=”RESOURCE_LOCAL” ? transaction-type="JTA"transaction-type=”RESOURCE_LOCAL”什么区别?

I also noticed some persistence.xml files with the transaction-type missing.我还注意到一些缺少事务类型的 persistence.xml 文件。 Is it correct?这是正确的吗?

Defaults默认值

Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment.在 JavaEE 环境中默认为JTA ,在 JavaSE 环境中默认为RESOURCE_LOCAL

RESOURCE_LOCAL资源_本地

With <persistence-unit transaction-type="RESOURCE_LOCAL"> you are responsible for EntityManager ( PersistenceContext/Cache ) creating and tracking使用<persistence-unit transaction-type="RESOURCE_LOCAL">您负责EntityManager ( PersistenceContext/Cache ) 创建和跟踪

  • You must use the EntityManagerFactory to get an EntityManager您必须使用EntityManagerFactory来获取EntityManager
  • The resulting EntityManager instance is a PersistenceContext/Cache An EntityManagerFactory can be injected via the @PersistenceUnit annotation only (not @PersistenceContext )生成的EntityManager实例是PersistenceContext/Cache EntityManagerFactory只能通过@PersistenceUnit注释注入(不是@PersistenceContext
  • You are not allowed to use @PersistenceContext to refer to a unit of type RESOURCE_LOCAL不允许使用@PersistenceContext来引用RESOURCE_LOCAL类型的单元
  • You must use the EntityTransaction API to begin/commit around every call to your EntityManger您必须使用EntityTransaction API 来开始/提交对EntityManger每次调用
  • Calling entityManagerFactory.createEntityManager() twice results in two separate EntityManager instances and therefor two separate PersistenceContexts/Caches .调用entityManagerFactory.createEntityManager()两次会导致两个单独的EntityManager实例,因此导致两个单独的PersistenceContexts/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实例几乎从来都不是一个好主意(不要创建第二个实例,除非您已经销毁了第一个实例)

JTA JTA

With <persistence-unit transaction-type="JTA"> the container will do EntityManager ( PersistenceContext/Cache ) creating and tracking.使用<persistence-unit transaction-type="JTA">容器将执行EntityManager ( PersistenceContext/Cache ) 创建和跟踪。

  • You cannot use the EntityManagerFactory to get an EntityManager您不能使用EntityManagerFactory来获取EntityManager
  • You can only get an EntityManager supplied by the container您只能获得容器提供的EntityManager
  • An EntityManager can be injected via the @PersistenceContext annotation only (not @PersistenceUnit ) EntityManager只能通过@PersistenceContext注释注入(不是@PersistenceUnit
  • You are not allowed to use @PersistenceUnit to refer to a unit of type JTA不允许使用@PersistenceUnit来引用 JTA 类型的单元
  • The EntityManager given by the container is a reference to the PersistenceContext/Cache associated with a JTA Transaction.容器提供的EntityManager是对与 JTA 事务关联的PersistenceContext/Cache的引用。
  • If no JTA transaction is in progress, the EntityManager cannot be used because there is no PersistenceContext/Cache .如果没有正在进行的 JTA 事务,则无法使用EntityManager因为没有PersistenceContext/Cache
  • Everyone with an EntityManager reference to the same unit in the same transaction will automatically have a reference to the same PersistenceContext/Cache每个在同一事务中拥有对同一单元的EntityManager引用的人都会自动拥有对同一PersistenceContext/Cache的引用
  • The PersistenceContext/Cache is flushed and cleared at JTA commit time PersistenceContext/Cache在 JTA 提交时被刷新和清除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM