persistence.xml 不同的事务类型属性 [英] persistence.xml different transaction-type attributes

查看:32
本文介绍了persistence.xml 不同的事务类型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于persistence.xml 不同的事务类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆