4.3. EXTENDED Persistence Context
All application managed entity manager and container managed persistence contexts defined as such are
EXTENDED
. This means that the persistence context type goes beyond the transaction life cycle. We should then understand what happens to operations made outside the scope of a transaction.
In an
EXTENDED
persistence context, all read only operations of the entity manager can be executed outside a transaction (find()
, getReference()
, refresh()
, and read queries). Some modifications operations can be executed outside a transaction, but they are queued until the persistence context join a transaction: this is the case of persist()
, merge()
, remove()
. Some operations cannot be called outside a transaction: flush()
, lock()
, and update/delete queries.
4.3.1. Container Managed Entity Manager
When using an EXTENDED persistence context with a container managed entity manager, the lifecycle of the persistence context is binded to the lifecycle of the Stateful Session Bean. Plus if the entity manager is created outside a transaction, modifications operations (persist, merge, remove) are queued in the persistence context and not executed to the database.
When a method of the stateful session bean involved or starting a transaction is later called, the entity manager join the transaction. All queued operation will then be executed to synchronize the persistence context.
This is perfect to implement the
entitymanager-per-conversation
pattern. A stateful session bean represents the conversation implementation. All intermediate conversation work will be processed in methods not involving transaction. The end of the conversation will be processed inside a JTA
transaction. Hence all queued operations will be executed to the database and commited. If you are interested in the notion of conversation inside your application, have a look at JBoss Seam. Jboss Seam emphasizes the concept of conversation and entity manager lifecycle and bind EJB3 and JSF together.