8.5.2 Persistence context when the container-managed EntityManager is used
When you use the container-managed EntityManager, the life cycle of the persistence context is managed by the container and the persistence context is automatically propagated along with the JTA transaction. You can choose the transaction scope persistence context or the extended persistence context as the type of the persistence context life cycle.
The following sections describe the respective persistence contexts:
- Organization of this subsection
(1) Transaction scope persistence context
With the JPA specifications, the persistence context having the same life cycle as the transaction is called the transaction scope persistence context.
The EntityManager has the same life cycle as that of the transaction by default. Therefore, the updates cached in the persistence context of EntityManager are applied to the database when the transaction is committed.
(a) Life cycle of persistence context
The life cycle of the transaction scope persistence context is as follows:
-
Creating the persistence context
The transaction scope persistence context is created when the container-managed EntityManager is first invoked in a JTA transaction.
The created persistence context is associated with the JTA transaction.
After that, when the container-managed EntityManager is used in the same JTA transaction, this persistence context is used.
-
Destroying the persistence context
When the JTA transaction is committed or rolled back, the transaction scope persistence context is destroyed.
If the container-managed EntityManager is invoked outside the transaction, all the entities loaded from the database are immediately detached when the invocation of the EntityManager method ends.
(2) Extended persistence context
With the Java EE environment, if EntityManager is used from the Stateful Session Bean, you can set the same persistence context lifetime as the Stateful Session Bean lifetime. In this case, the updates are applied to the database every time the transaction is committed, but the entity objects managed in the persistence context are stored in the managed status as are across multiple transactions. The persistence context with the same life cycle as the Stateful Session Bean is called the extended persistence context in the JPA.
The extended persistence context is created simultaneously when the Stateful Session Bean is created and is associated with that Stateful Session Bean. Thereafter, the extended persistence context is destroyed simultaneously when the Stateful Session Bean is destroyed.
When the Stateful Session Bean creates another Stateful Session Bean and when the definition is such that the creating Stateful Session Bean and the created Stateful Session Bean both use the extended persistence context, the persistence context on the creating side is inherited on the created machine. The persistence context is inherited regardless of whether the transaction is active when the Stateful Session Bean is created. If the persistence context is inherited when the Stateful Session Bean is created, the persistence context is destroyed when all the Stateful Session Beans sharing that persistence context are destroyed.
(3) Extended persistence context and transactions
The extended persistence context exists from the time the EntityManager instance is created until the instance is closed. The extended persistence context supports multiple transactions and the invocation outside the EntityManager transaction.
The relationship with transactions is as follows:
-
If EntityManager is invoked within the transaction scope or if the stateful session beans bound by the persistence context are invoked in the transaction scope, the entities managed by EntityManager participate in the transaction.
-
Regardless of whether the transaction is running, the persist, remove, merge, and refresh operations might be performed. In this case, EntityManager participates in the transaction and the updates are applied in the database when the transaction is committed.
-
Even after the transaction is committed, the references to the entity object are stored. The entity object is managed by EntityManager and is updated as an object managed between transactions (managed entity).
(4) Propagation of persistence context
When the container-managed EntityManager is used, the persistence context is propagated using the JTA transaction and might be associated with multiple EntityManager. However, the persistence context is only propagated with the same Application Server. The persistence context is not propagated in a remote Application Server.
The following points separately describe the propagation of the persistence context for the states when a component is invoked:
- If the JTA transaction does not exist or the persistence context is not associated with the JTA transaction when the component is invoked
-
The persistence context is not propagated. The operations when EntityManager is invoked from this component are as follows:
-
When EntityManager using the transaction scope persistence context is invoked, a new persistence context is created.
-
When EntityManager using the extended persistence context is invoked, the extended persistence context associated with the invoked Stateful Session Bean is used.
-
If the JTA transaction exists when EntityManager is invoked, the persistence context is associated with the JTA transaction.
-
- If the JTA transaction is propagated and the persistence context is associated with the JTA transaction when the component is invoked
-
The operations when EntityManager is invoked from this component are as follows:
-
If another persistence context is associated with the JTA transaction although the component is the Stateful Session Bean that already has the extended persistence context, the container throws EJBException.
-
When EntityManager using the transaction scope persistence context is invoked, the persistence context associated with the propagated JTA transaction is used.
-