Search in sources :

Example 1 with PhysicalEntityManagerWrapper

use of com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper in project Payara by payara.

the class AsynchronousTask method destroyExtendedEMsForContext.

private void destroyExtendedEMsForContext(SessionContextImpl sc) {
    for (PhysicalEntityManagerWrapper emWrapper : sc.getExtendedEntityManagers()) {
        synchronized (extendedEMReferenceCountMap) {
            EntityManager em = emWrapper.getEM();
            if (extendedEMReferenceCountMap.containsKey(em)) {
                EEMRefInfo refInfo = extendedEMReferenceCountMap.get(em);
                if (refInfo.refCount > 1) {
                    refInfo.refCount--;
                    _logger.log(Level.FINE, "Decremented RefCount ExtendedEM em: " + em);
                } else {
                    _logger.log(Level.FINE, "DESTROYED ExtendedEM em: " + em);
                    refInfo = extendedEMReferenceCountMap.remove(em);
                    eemKey2EEMMap.remove(refInfo.getKey());
                    try {
                        em.close();
                    } catch (Throwable th) {
                        _logger.log(Level.FINE, "Exception during em.close()", th);
                    }
                }
            }
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PhysicalEntityManagerWrapper(com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper)

Example 2 with PhysicalEntityManagerWrapper

use of com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper in project Payara by payara.

the class AsynchronousTask method validateEMForClientTx.

@Override
protected void validateEMForClientTx(EjbInvocation inv, JavaEETransaction clientJ2EETx) throws EJBException {
    SessionContextImpl sessionCtx = (SessionContextImpl) inv.context;
    Map<EntityManagerFactory, PhysicalEntityManagerWrapper> entityManagerMap = sessionCtx.getExtendedEntityManagerMap();
    for (Map.Entry<EntityManagerFactory, PhysicalEntityManagerWrapper> entry : entityManagerMap.entrySet()) {
        EntityManagerFactory emf = entry.getKey();
        // Extended persistence context for the propagated transaction.
        if (clientJ2EETx.getTxEntityManagerResource(emf) != null) {
            throw new EJBException("There is an active transactional persistence context for the same EntityManagerFactory as the current stateful session bean's extended persistence context");
        }
        // Now see if there's already a *different* extended
        // persistence context within this transaction for the
        // same EntityManagerFactory.
        PhysicalEntityManagerWrapper physicalEM = (PhysicalEntityManagerWrapper) clientJ2EETx.getExtendedEntityManagerResource(emf);
        if ((physicalEM != null) && entry.getValue().getEM() != physicalEM.getEM()) {
            throw new EJBException("Detected two different extended persistence contexts for the same EntityManagerFactory within a transaction");
        }
    }
}
Also used : PhysicalEntityManagerWrapper(com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper) EntityManagerFactory(javax.persistence.EntityManagerFactory) EJBException(javax.ejb.EJBException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with PhysicalEntityManagerWrapper

use of com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper in project Payara by payara.

the class AsynchronousTask method enlistExtendedEntityManagers.

@Override
protected void enlistExtendedEntityManagers(ComponentContext ctx) {
    if (ctx.getTransaction() != null) {
        JavaEETransaction j2eeTx = (JavaEETransaction) ctx.getTransaction();
        SessionContextImpl sessionCtx = (SessionContextImpl) ctx;
        Map<EntityManagerFactory, PhysicalEntityManagerWrapper> entityManagerMap = sessionCtx.getExtendedEntityManagerMap();
        for (Map.Entry<EntityManagerFactory, PhysicalEntityManagerWrapper> entry : entityManagerMap.entrySet()) {
            EntityManagerFactory emf = entry.getKey();
            PhysicalEntityManagerWrapper extendedEm = entry.getValue();
            PhysicalEntityManagerWrapper extendedEmAssociatedWithTx = EntityManagerWrapper.getExtendedEntityManager(j2eeTx, emf);
            // this extended EntityManagerFactory within the current tx
            if (extendedEmAssociatedWithTx == null) {
                j2eeTx.addExtendedEntityManagerMapping(emf, extendedEm);
                sessionCtx.setEmfRegisteredWithTx(emf, true);
                // entity manager with the transaction.
                if (extendedEm.getSynchronizationType() == SYNCHRONIZED) {
                    extendedEm.getEM().joinTransaction();
                }
            }
        }
    }
}
Also used : JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) PhysicalEntityManagerWrapper(com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper) EntityManagerFactory(javax.persistence.EntityManagerFactory) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with PhysicalEntityManagerWrapper

use of com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper in project Payara by payara.

the class AsynchronousTask method findExtendedEMFromInvList.

private PhysicalEntityManagerWrapper findExtendedEMFromInvList(EntityManagerFactory emf) {
    PhysicalEntityManagerWrapper em = null;
    ComponentInvocation compInv = (ComponentInvocation) invocationManager.getCurrentInvocation();
    if (compInv != null) {
        if (compInv.getInvocationType() == ComponentInvocation.ComponentInvocationType.EJB_INVOCATION) {
            EjbInvocation ejbInv = (EjbInvocation) compInv;
            if (ejbInv.context instanceof SessionContextImpl) {
                SessionContextImpl ctxImpl = (SessionContextImpl) ejbInv.context;
                if (ctxImpl.container instanceof StatefulSessionContainer) {
                    em = ctxImpl.getExtendedEntityManager(emf);
                }
            }
        }
    }
    return em;
}
Also used : EjbInvocation(com.sun.ejb.EjbInvocation) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) PhysicalEntityManagerWrapper(com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper)

Example 5 with PhysicalEntityManagerWrapper

use of com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper in project Payara by payara.

the class AsynchronousTask method delistExtendedEntityManagers.

@Override
protected void delistExtendedEntityManagers(ComponentContext ctx) {
    if (ctx.getTransaction() != null) {
        SessionContextImpl sessionCtx = (SessionContextImpl) ctx;
        JavaEETransaction j2eeTx = (JavaEETransaction) sessionCtx.getTransaction();
        Map<EntityManagerFactory, PhysicalEntityManagerWrapper> entityManagerMap = sessionCtx.getExtendedEntityManagerMap();
        for (Map.Entry<EntityManagerFactory, PhysicalEntityManagerWrapper> entry : entityManagerMap.entrySet()) {
            EntityManagerFactory emf = entry.getKey();
            if (sessionCtx.isEmfRegisteredWithTx(emf)) {
                j2eeTx.removeExtendedEntityManagerMapping(emf);
                sessionCtx.setEmfRegisteredWithTx(emf, false);
            }
        }
    }
}
Also used : JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) PhysicalEntityManagerWrapper(com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper) EntityManagerFactory(javax.persistence.EntityManagerFactory) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

PhysicalEntityManagerWrapper (com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper)6 EntityManagerFactory (javax.persistence.EntityManagerFactory)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JavaEETransaction (com.sun.enterprise.transaction.api.JavaEETransaction)2 EJBException (javax.ejb.EJBException)2 EntityManager (javax.persistence.EntityManager)2 EjbInvocation (com.sun.ejb.EjbInvocation)1 EntityManagerReferenceDescriptor (com.sun.enterprise.deployment.EntityManagerReferenceDescriptor)1 HashSet (java.util.HashSet)1 ComponentInvocation (org.glassfish.api.invocation.ComponentInvocation)1