Java 事务范围的持久化上下文和扩展持久化上下文有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2547817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:08:58  来源:igfitidea点击:

What is the difference between Transaction-scoped Persistence context and Extended Persistence context?

javajpa

提问by Muhammad Hewedy

What is the difference between Transaction-scoped Persistence context and Extended Persistence context?

事务范围的持久化上下文和扩展持久化上下文有什么区别?

采纳答案by Pascal Thivent

The difference is clearly explained in the JSR-220Enterprise JavaBeans 3.0 specification:

JSR-220Enterprise JavaBeans 3.0 规范中清楚地解释了差异:

5.6 Container-managed Persistence Contexts

(...)

A container-managed persistence context may be defined to have either a lifetime that is scoped to a single transaction or an extended lifetime that spans multiple transactions, depending on the PersistenceContextTypethat is specified when its EntityManageris created. This specification refers to such persistence contexts as transaction-scoped persistence contextsand extended persistence contextsrespectively.

(...)

5.6.1 Container-managed Transaction-scoped Persistence Context

The application may obtain a container-managed entity manager with transaction-scoped persistence context bound to the JTA transaction by injection or direct lookup in the JNDI namespace. The persistence context type for the entity manager is defaulted or defined as PersistenceContextType.TRANSACTION.

A new persistence context begins when the container-managed entity manager is invoked[36]in the scope of an active JTA transaction, and there is no current persistence context already associated with the JTA transaction. The persistence context is created and then associated with the JTA transaction.

The persistence context ends when the associated JTA transaction commits or rolls back, and all entities that were managed by the EntityManager become detached.

If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database will immediately become detached at the end of the method call.

5.6.2 Container-managed Extended Persistence Context

A container-managed extended persistence context can only be initiated within the scope of a stateful session bean. It exists from the point at which the stateful session bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDEDis created, and is said to be bound to the stateful session bean. The dependency on the extended persistence context is declared by means of the PersistenceContextannotation or persistence-context-ref deployment descriptor element.

The persistence context is closed by the container when the @Removemethod of the stateful session bean completes (or the stateful session bean instance is otherwise destroyed).

(...)

5.6 容器管理的持久化上下文

(……)

容器管理的持久性上下文可以定义为具有限定于单个事务的生命周期或跨越多个事务的扩展生命周期,具体取决于 创建PersistenceContextType时指定的EntityManager。本规范将此类持久性上下文分别称为 事务范围的持久性上下文扩展的持久性上下文

(……)

5.6.1 容器管理的事务范围持久化上下文

应用程序可以通过在 JNDI 名称空间中注入或直接查找,获得一个容器管理的实体管理器,该实体管理器具有绑定到 JTA 事务的事务范围持久上下文。实体管理器的持久化上下文类型默认或定义为 PersistenceContextType.TRANSACTION.

当容器管理的实体管理器在活动 JTA 事务的范围内被调用[36]时,新的持久性上下文开始,并且当前没有与 JTA 事务相关联的持久性上下文。持久性上下文被创建,然后与 JTA 事务相关联。

当关联的 JTA 事务提交或回滚时,持久性上下文结束,并且由 EntityManager 管理的所有实体都变得分离。

如果在事务范围之外调用实体管理器,则从数据库加载的任何实体将在方法调用结束时立即分离。

5.6.2 容器管理的扩展持久化上下文

容器管理的扩展持久性上下文只能在有状态会话 bean 的范围内启动。它存在于声明对实体管理器类型的依赖关系的有状态会话 bean 的PersistenceContextType.EXTENDED创建点,并被称为绑定到有状态会话 bean。对扩展持久性上下文的依赖是通过 PersistenceContext注解或persistence-context-ref 部署描述符元素声明的。

@Remove有状态会话 bean的方法完成(或者有状态会话 bean 实例以其他方式被销毁)时,持久性上下文由容器关闭。

(……)

回答by Pascal Thivent

Transaction scoped persistence context

事务范围的持久化上下文

As the name suggests, a transaction-scoped persistence context is tied to the lifecycle of the transaction. It is created by the container during a transaction and will be closed when the transaction completes.

顾名思义,事务范围的持久化上下文与事务的生命周期相关联。它由容器在事务期间创建,并在事务完成时关闭。

Transaction-scoped entity managers are responsible for creating transaction-scoped persistence contexts automatically when needed. We say only when needed because transactionscoped persistence context creation is lazy.

事务范围的实体管理器负责在需要时自动创建事务范围的持久性上下文。我们只在需要时说,因为事务范围的持久性上下文创建是惰性的。

An entity manager will create a persistence context only when a method is invoked on the entity manager and when there is no persistence context available.

仅当在实体管理器上调用方法并且没有可用的持久上下文时,实体管理器才会创建持久上下文。

Extended Persistence Contexts

扩展持久性上下文

The lifecycle of an extended persistence context is tied to the stateful session bean to which it is bound.

扩展持久性上下文的生命周期与其绑定的有状态会话 bean 相关联。

Unlike a transaction-scoped entity manager that creates a new persistence context for each transaction, the extended entity manager of a stateful session bean always uses the same persistence context.

与为每个事务创建新持久性上下文的事务范围实体管理器不同,有状态会话 bean 的扩展实体管理器始终使用相同的持久性上下文。

The stateful session bean is associated with a single extended persistence context that is created when the bean instance is created and closed when the bean instance is removed. This has implications for both the association and propagation characteristics of the extended persistence context.

有状态会话 bean 与单个扩展持久性上下文相关联,该上下文在创建 bean 实例时创建并在删除 bean 实例时关闭。这对扩展持久性上下文的关联和传播特性都有影响。

回答by mika

There are lot of details to respect... but to keep it short I remember the difference like this:

有很多细节需要尊重......但为了简短起见,我记得这样的区别:

Transaction-Scoped Persistence Context

事务范围的持久化上下文

In short:When a method on a transaction-scoped bean is called, a transaction will automatically be started by the container and a new persistence context will be created for you. When the method ends the transactions ends and the persistence context will be closed, your entities will become detached.

简而言之:当调用事务范围 bean 上的方法时,容器将自动启动一个事务,并为您创建一个新的持久性上下文。当方法结束事务结束并且持久性上下文将关闭时,您的实体将分离。

Benefit:This behaviour is stateless, doesn't need much maintenance in the code by you and makes your EntityManager threadsafe.

好处:这种行为是无状态的,不需要您在代码中进行太多维护,并使您的 EntityManager 线程安全。

Extended Persistence Context

扩展持久性上下文

In short:Can be used for a stateful session bean only and is tied to the lifecycle of the bean. The persistence context can spawn accross multiple transactions, which means the methods in your extended bean share the same persistence context.

简而言之:只能用于有状态会话 bean,并且与 bean 的生命周期相关联。持久性上下文可以跨多个事务产生,这意味着扩展 bean 中的方法共享相同的持久性上下文。

Benefit:Perfect to implement a conversation style interaction with clients. Your client call several bean methods to tell your bean all the information you need to know and at the end of the conversation you persist everything to your DB.

优点:非常适合与客户实现对话式交互。您的客户端调用多个 bean 方法来告诉您的 bean 您需要知道的所有信息,并在对话结束时将所有信息保存到您的数据库中。

Important things to know

重要的事情要知道

Transaction propagation:Assuming default TransactionAttributesfor a transaction-scoped bean with two methods A and B.

事务传播:假设事务范围 bean 的默认TransactionAttributes具有两种方法 A 和 B。

If method B is called inside of method A, you can propagate A's persistence context to B. That way method B can access even non yet persisted entities that where created/changed by A, because they are still managed by the persistence context on which B has now access on.

如果在方法 A 内部调用方法 B,则可以将 A 的持久性上下文传播到 B。这样方法 B 甚至可以访问由 A 创建/更改的尚未持久化的实体,因为它们仍然由 B 所在的持久性上下文管理现在可以访问了。

Transaction propagation from extended to transaction-scoped:You can propagate the persistence context of an extended bean to a transaction-scoped bean by calling a method of your transaction-scoped bean from your extended bean. With default transaction attribute (REQUIRED) your transaction-scoped bean will re-use the already existing, active persistence context of your extended bean.

扩展到事务范围的事务传播:您可以通过从扩展 bean 调用事务范围 bean 的方法,将扩展 bean 的持久性上下文传播到事务范围 bean。使用默认事务属性 ( REQUIRED),您的事务范围 bean 将重用扩展 bean 的已存在的、活动的持久性上下文。

Transaction propagation from transaction-scoped to extended:The other way around however is not as intuitive, because an extended persistence context always tries to make itself the active persistence context. You have to change the default transaction attribute for the extended bean using @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW). This will suspend any active transaction (associated with a persistence context) before the extended bean method starts.

事务范围到扩展的事务传播:然而,相反的方式并不直观,因为扩展的持久性上下文总是试图使自己成为活动的持久性上下文。您必须使用 更改扩展 bean 的默认事务属性@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)。这将在扩展 bean 方法启动之前挂起任何活动事务(与持久性上下文相关联)。