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

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

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
PersistenceContextType that is
specified when its EntityManager is
created. This specification refers to
such persistence contexts as
transaction-scoped persistence contexts and extended persistence
contexts
respectively.

(…)

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.EXTENDED
is 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
PersistenceContext annotation or
persistence-context-ref deployment
descriptor element.

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

(…)

Leave a Comment