Java 持久性单元作为 RESOURCE_LOCAL 还是 JTA?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1962525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persistence unit as RESOURCE_LOCAL or JTA?
提问by cometta
I have queries as below:
我有以下疑问:
- What is the difference of these two?
- Are both of these supported by all databases?
- Are JPA TransactionManager and JTA TransactionManager different?
- 这两者有什么区别?
- 所有数据库都支持这两个吗?
- JPA TransactionManager 和 JTA TransactionManager 有区别吗?
采纳答案by skaffman
JPA implementations have the choice of managing transactions themselves (RESOURCE_LOCAL
), or having them managed by the application server's JTA implementation.
JPA 实现可以选择自己管理事务 ( RESOURCE_LOCAL
),或者让它们由应用程序服务器的 JTA 实现管理。
In most cases, RESOURCE_LOCAL is fine. This would use basic JDBC-level transactions. The downside is that the transaction is local to the JPA persistence unit, so if you want a transaction that spans multiple persistence units (or other databases), then RESOURCE_LOCAL may not be good enough.
在大多数情况下,RESOURCE_LOCAL 没问题。这将使用基本的 JDBC 级事务。缺点是事务对于 JPA 持久化单元是本地的,所以如果你想要一个跨越多个持久化单元(或其他数据库)的事务,那么 RESOURCE_LOCAL 可能不够好。
JTA is also used for managing transactions across systems like JMS and JCA, but that's fairly exotic usage for most of us.
JTA 还用于管理跨系统(如 JMS 和 JCA)的事务,但这对我们大多数人来说是相当奇特的用法。
To use JTA, you need support for it in your application server, and also support from the JDBC driver.
要使用 JTA,您需要在您的应用服务器中支持它,并且还需要来自 JDBC 驱动程序的支持。
回答by Chandra Patni
Resource_Local and JTA are transaction managers (methods of doing transactions). This is not the property of database but the component responsible for coordinating transactions. JPA and JTA transaction managers are different. JPA transaction manager is responsible for JPA transactions and you want to use one if you are only doing JPA transaction. JTA transaction manager is general purpose transaction manager and can enlist other resources such as JMS queues in transaction. Typically Java EE containers employ a JTA transaction manager for EJBs, JPA entities, etc.
Resource_Local 和 JTA 是事务管理器(进行事务的方法)。这不是数据库的属性,而是负责协调事务的组件。JPA 和 JTA 事务管理器是不同的。JPA 事务管理器负责 JPA 事务,如果您只进行 JPA 事务,您想使用一个。JTA 事务管理器是通用事务管理器,可以在事务中征用其他资源,例如 JMS 队列。通常,Java EE 容器为 EJB、JPA 实体等使用 JTA 事务管理器。
回答by informatik01
As an addition to other answers
作为其他答案的补充
Here is an excerptfrom the extremely useful article (published on the Apache TomEEwebsite), which can also help answer the OP's first question (the link to the article is below).
这是一篇非常有用的文章(发表在Apache TomEE网站上)的摘录,它也可以帮助回答 OP 的第一个问题(文章链接如下)。
Comparing RESOURCE_LOCAL and JTA persistence contexts
With <persistence-unit transaction-type="RESOURCE_LOCAL">YOUare responsible for EntityManager (PersistenceContext/Cache) creating and tracking...
- You mustuse the EntityManagerFactoryto get an EntityManager
- The resulting EntityManagerinstance isa PersistenceContext/Cache
- An EntityManagerFactorycan be injected via the @PersistenceUnitannotation only (not @PersistenceContext)
- You are notallowed to use @PersistenceContext to refer to a unit of type RESOURCE_LOCAL
- You mustuse the EntityTransactionAPI to begin/commit around everycall to your EntityManger
- Calling entityManagerFactory.createEntityManager() twice results in twoseparate EntityManager instances and therefor twoseparate PersistenceContexts/Caches.
- It is almost nevera good idea to have more than one instanceof an EntityManager in use (don't create a second one unless you've destroyed the first)
With <persistence-unit transaction-type="JTA">the CONTAINERwill do EntityManager (PersistenceContext/Cache) creating and tracking...
- You cannotuse the EntityManagerFactoryto get an EntityManager
- You can only get an EntityManagersupplied by the container
- An EntityManagercan be injected via the @PersistenceContextannotation only (not @PersistenceUnit)
- You are notallowed to use @PersistenceUnit to refer to a unit of type JTA
- The EntityManagergiven by the container is a referenceto the PersistenceContext/Cache associated with a JTA Transaction.
- If no JTA transaction is in progress, the EntityManager cannot be usedbecause there is no PersistenceContext/Cache.
- Everyone with an EntityManager reference to the same unitin the same transactionwill automatically have a reference to the same PersistenceContext/Cache
- The PersistenceContext/Cache is flushedand cleared at JTA committime
比较 RESOURCE_LOCAL 和 JTA 持久化上下文
使用<persistence-unit transaction-type="RESOURCE_LOCAL">您负责 EntityManager (PersistenceContext/Cache) 创建和跟踪...
- 您必须使用 EntityManagerFactory来获取 EntityManager
- 生成的EntityManager实例 是一个 PersistenceContext/Cache
- 一个 的EntityManagerFactory可以通过被注入 @PersistenceUnit注解只有(未@PersistenceContext)
- 你是不是允许使用@PersistenceContext引用类型的单位RESOURCE_LOCAL
- 您必须使用 EntityTransactionAPI 来开始/提交 对 EntityManger 的每次调用
- 调用 entityManagerFactory.createEntityManager() 两次会导致 两个单独的 EntityManager 实例和 两个单独的 PersistenceContexts/Caches。
- 这是几乎从未有多于一个好主意, 比如在使用一个EntityManager的(不创建除非你破坏了第一第二个)
与<持久单元交易类型=“JTA”>所述 容器会做的EntityManager(PersistenceContext /高速缓存)创建和跟踪...
- 您不能使用 EntityManagerFactory来获取 EntityManager
- 您只能获得容器提供 的EntityManager
- 一个的EntityManager可以通过注入@PersistenceContext注解只有(未@PersistenceUnit)
- 你是 不是允许使用@PersistenceUnit引用类型JTA单位
- 容器提供的EntityManager是对与 JTA 事务关联的 PersistenceContext/Cache的引用。
- 如果没有正在进行的 JTA 事务,则无法使用EntityManager, 因为没有 PersistenceContext/Cache。
- 每个在同一事务中拥有对同一单元的 EntityManager 引用的人都会自动拥有对同一 PersistenceContext/Cache的引用
- PersistenceContext/Cache在 JTA提交时被刷新和清除
Anyone interested in learning the Java Persistence API - please do yourself a favor and read the full article here: JPA Concepts: JPA 101.
任何有兴趣学习 Java Persistence API 的人 - 请帮自己一个忙,并在此处阅读全文:JPA 概念:JPA 101。
回答by Pushpendra
resource_local vs JTA its about local transaction vs global transaction. It's about can we manage multiple resources under a single transaction.
resource_local vs JTA 关于本地事务 vs 全局事务。这是关于我们能否在单个事务下管理多个资源。
CMT vs BMT its about who is opening and closing transaction - application developer or application server.
CMT vs BMT 关于谁打开和关闭事务 - 应用程序开发人员或应用程序服务器。