EntityManager delegate;
if( contextType == PersistenceContextType.TRANSACTION ) {
JavaEETransaction tx = getCurrentTransaction();
if( tx != null ) {
// If there is an active extended persistence context
// for the same entity manager factory and the same tx,
// it takes precedence.
PhysicalEntityManagerWrapper propagatedPersistenceContext = getExtendedEntityManager(tx, entityManagerFactory);
if(propagatedPersistenceContext == null) {
propagatedPersistenceContext = getTxEntityManager(tx, entityManagerFactory);
if( propagatedPersistenceContext == null ) {
// If there is a transaction and this is the first
// access of the wrapped entity manager, create an
// actual entity manager and associate it with the
// entity manager factory.
EntityManager em = entityManagerFactory.createEntityManager(synchronizationType, emProperties);
propagatedPersistenceContext = new PhysicalEntityManagerWrapper(em, synchronizationType);
tx.addTxEntityManagerMapping(entityManagerFactory, propagatedPersistenceContext);
} else {
//Check if sync type of current persistence context is compatible with persistence context being propagated
if(synchronizationType == SYNCHRONIZED && propagatedPersistenceContext.getSynchronizationType() == UNSYNCHRONIZED) {
throw new IllegalStateException("Detected an UNSYNCHRONIZED persistence context being propagated to SYNCHRONIZED persistence context.");