Java Reference
In-Depth Information
Persistence context synchronization
BeforeJPA2.1,thecontainer-managedpersistencecontextwasautomaticallyjoined
to the current transaction, and any update made to the persistence context was
propagated to the underlying resource manager. With the new specification, it is now
possible to have a persistence context that will not be automatically enlisted in any
JTA transaction. This can be done by simply creating a container-managed entity
manager of synchronization type SynchronizationType.UNSYNCHRONIZED as
shown in the following code.
Creation and enlistment of a SynchronizationType.UNSYNCHRONIZED persist-
ence context:
@Stateless
@LocalBean
public class MySessionBean {
/* Creation of an entity manager for
* unsynchronized persistence context
*/
@PersistenceContext(synchronization =
SynchronizationType.UNSYNCHRONIZED)
EntityManager em;
public void useUnsynchronizedEntityManager(){
//enlisting of an unsynchronized
persistence context
em.joinTransaction();
//...
}
}
In the preceding code, you will notice that we called the EntityMan-
ager.joinTransaction() method; this is justified by the fact that a persistence
context of type SynchronizationType.UNSYNCHRONIZED is enlisted in a JTA
transaction only after calling the EntityManager.joinTransaction() method,
and after a commit or rollback, the SynchronizationType.UNSYNCHRONIZED
Search WWH ::




Custom Search