getTransaction().begin()/.commit() 和 joinTransaction() 与 JPA2/EclipseLink 之间有区别吗?

发布于 2024-12-03 06:33:47 字数 328 浏览 4 评论 0原文

之间是否有区别

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2024-12-10 06:33:47

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) whereas EntityTransaction.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-managed EntityManager instances when you need JTA transaction support. Container-managed EntityManagers 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-managed EntityManagers that you need to join an existing transaction. Application managed EntityManagers are not injected by the container; instead the container may only inject an EntityManagerFactory instance (using the @PersistenceUnit annotation) that is used by the application to obtain the EntityManager 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 until EntityTransaction.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文