Search in sources :

Example 1 with JavaEEContainer

use of com.sun.enterprise.container.common.spi.JavaEEContainer in project Payara by payara.

the class EntityManagerWrapper method _getDelegate.

private EntityManager _getDelegate() {
    if (entityManagerFactory == null) {
        init();
    }
    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.");
                    }
                }
            }
            delegate = propagatedPersistenceContext.getEM();
        } else {
            // Get non transactional entity manager corresponding to this wrapper from current invocation
            delegate = getNonTxEMFromCurrentInvocation();
        }
    } else {
        if (extendedEntityManager == null) {
            ComponentInvocation ci = invMgr.getCurrentInvocation();
            if (ci != null) {
                Object cc = ci.getContainer();
                if (cc instanceof JavaEEContainer) {
                    extendedEntityManager = ((JavaEEContainer) cc).lookupExtendedEntityManager(entityManagerFactory);
                }
            }
        }
        delegate = extendedEntityManager;
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("In EntityManagerWrapper::_getDelegate(). " + "Logical entity manager  = " + this);
        _logger.fine("Physical entity manager = " + delegate);
    }
    return delegate;
}
Also used : JavaEEContainer(com.sun.enterprise.container.common.spi.JavaEEContainer) JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation)

Aggregations

JavaEEContainer (com.sun.enterprise.container.common.spi.JavaEEContainer)1 JavaEETransaction (com.sun.enterprise.transaction.api.JavaEETransaction)1 ComponentInvocation (org.glassfish.api.invocation.ComponentInvocation)1