getTransaction().begin()/.commit() 和 joinTransaction() 与 JPA2/EclipseLink 之间有区别吗?
之间是否有区别
EntityManager em = ...
// Some action
em.joinTransaction();
em.close();
我正在使用带注释的 JPA2 和 EclipseLink,我想知道和
EntityManager em = PU.entityManager();
em.getTransaction().begin();
// Some action
em.getTransaction().commit();
em.close();
,我通常应该更喜欢哪一个?
I'm using JPA2 with annotations and EclipseLink and I'm wondering if there is a difference between
EntityManager em = ...
// Some action
em.joinTransaction();
em.close();
and
EntityManager em = PU.entityManager();
em.getTransaction().begin();
// Some action
em.getTransaction().commit();
em.close();
and which one should I generally prefer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EntityManager.joinTransaction
用于 JTA 事务(意味着持久化单元配置为使用 JTA 事务),而EntityTransaction.begin
< /a> 用于资源本地实体管理器(意味着不使用 JTA 来管理事务)。因此,它们都适用于不同的场景。第一种情况,即
EntityManager.joinTransaction
很少使用,因为当您需要 JTA 事务支持时,您经常会注入容器管理的EntityManager
实例。容器管理的 EntityManager 被注入(使用@PersistenceContext
注释)到现有 JTA 事务的上下文中(由容器管理),因此无需显式加入事务。仅当出现应用程序管理的 EntityManager 时,您才需要加入现有事务。应用程序管理的 EntityManager 不是由容器注入的;相反,容器只能注入一个 EntityManagerFactory 实例(使用@PersistenceUnit
注释),应用程序使用它来获取EntityManager
实例。在第二种情况下,即
EntityTransaction.begin
,不会使用任何 JTA 事务来确定任何事务工作的范围。相反,事务是资源本地的,因为对持久性上下文所做的任何更改都将作为原子单元进行跟踪,直到调用 EntityTransaction.commit 为止。人们很少会在 Java EE 应用程序中使用资源本地实体管理器,因为您通常希望 EJB(和 EJB 容器)来划分事务边界,而不是应用程序源代码。此外,使用资源本地事务执行的任何事务工作都不会被可能已经启动的 JTA 事务跟踪,从而导致应用程序中的事务活动出现模糊、混乱和不明确的行为。EntityManager.joinTransaction
is meant to be used for JTA transactions (implying that the persistence unit is configured to use JTA transactions) whereasEntityTransaction.begin
is meant to be used for resource-local entity managers (implying the non-usage of JTA to manage transactions). Therefore, they're both meant to be used in different scenarios.The first case, i.e.
EntityManager.joinTransaction
is rarely used, for you would often inject container-managedEntityManager
instances when you need JTA transaction support. Container-managedEntityManager
s are injected (using the@PersistenceContext
annotation) into the context of an existing JTA transaction (managed by the container) and hence there is no need to explicitly join a transaction. It is only in the event of application-managedEntityManager
s that you need to join an existing transaction. Application managedEntityManager
s are not injected by the container; instead the container may only inject anEntityManagerFactory
instance (using the@PersistenceUnit
annotation) that is used by the application to obtain theEntityManager
instance.In the second case, i.e.
EntityTransaction.begin
, no JTA transactions will be used to scope any transactional work. Instead, the transaction is resource-local in that any changes made to the persistence context are tracked as an atomic unit untilEntityTransaction.commit
is invoked. One would rarely use resource-local entity managers in a Java EE application, as you would typically want EJBs (and the EJB container) to demarcate the transaction boundaries, and not the application source code. Also, any transactional work performed using a resource-local transaction will not be tracked by a JTA transaction that might already have been initiated, thereby resulting in vague , confusing and ambiguous behavior regarding the transactional activities in your application.