Search in sources :

Example 6 with PhysicalEntityManagerWrapper

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

the class AsynchronousTask method createExtendedEMs.

private void createExtendedEMs(SessionContextImpl ctx, Object sessionKey) {
    Set<EntityManagerReferenceDescriptor> emRefs = ejbDescriptor.getEntityManagerReferenceDescriptors();
    Iterator<EntityManagerReferenceDescriptor> iter = emRefs.iterator();
    Set<EEMRefInfo> eemRefInfos = new HashSet<EEMRefInfo>();
    while (iter.hasNext()) {
        EntityManagerReferenceDescriptor refDesc = iter.next();
        if (refDesc.getPersistenceContextType() == PersistenceContextType.EXTENDED) {
            String unitName = refDesc.getUnitName();
            EntityManagerFactory emf = EntityManagerFactoryWrapper.lookupEntityManagerFactory(ComponentInvocation.ComponentInvocationType.EJB_INVOCATION, unitName, ejbDescriptor);
            if (emf != null) {
                PhysicalEntityManagerWrapper physicalEntityManagerWrapper = findExtendedEMFromInvList(emf);
                if (physicalEntityManagerWrapper == null) {
                    // We did not find an extended EM that we can inherit from. Create one
                    try {
                        EntityManager em = emf.createEntityManager(refDesc.getSynchronizationType(), refDesc.getProperties());
                        physicalEntityManagerWrapper = new PhysicalEntityManagerWrapper(em, refDesc.getSynchronizationType());
                    } catch (Throwable th) {
                        EJBException ejbEx = new EJBException("Couldn't create EntityManager for" + " refName: " + refDesc.getName() + "; unitname: " + unitName);
                        ejbEx.initCause(th);
                        throw ejbEx;
                    }
                } else {
                    // We found an extended EM we can inherit from. Validate that sync type matches
                    if (physicalEntityManagerWrapper.getSynchronizationType() != refDesc.getSynchronizationType()) {
                        throw new EJBException("The current invocation inherits a persistence context of synchronization type '" + physicalEntityManagerWrapper.getSynchronizationType() + "' where as it references a persistence context of synchronization type '" + refDesc.getSynchronizationType() + "' refName: " + refDesc.getName() + " unitName: " + unitName);
                    }
                }
                String emRefName = refDesc.getName();
                long containerID = this.getContainerId();
                EEMRefInfo refInfo = null;
                synchronized (extendedEMReferenceCountMap) {
                    refInfo = extendedEMReferenceCountMap.get(physicalEntityManagerWrapper.getEM());
                    if (refInfo != null) {
                        refInfo.refCount++;
                    } else {
                        refInfo = new EEMRefInfo(emRefName, refDesc.getUnitName(), refDesc.getSynchronizationType(), containerID, sessionKey, physicalEntityManagerWrapper.getEM(), emf);
                        refInfo.refCount = 1;
                        extendedEMReferenceCountMap.put(physicalEntityManagerWrapper.getEM(), refInfo);
                        eemKey2EEMMap.put(refInfo.getKey(), refInfo.getEntityManager());
                    }
                }
                ctx.addExtendedEntityManagerMapping(emf, refInfo);
                eemRefInfos.add(refInfo);
            } else {
                throw new EJBException("EMF is null. Couldn't get extended EntityManager for" + " refName: " + refDesc.getName() + "; unitname: " + unitName);
            }
        }
    }
    if (eemRefInfos.size() > 0) {
        ctx.setEEMRefInfos(eemRefInfos);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PhysicalEntityManagerWrapper(com.sun.enterprise.container.common.impl.PhysicalEntityManagerWrapper) EntityManagerFactory(javax.persistence.EntityManagerFactory) EJBException(javax.ejb.EJBException) EntityManagerReferenceDescriptor(com.sun.enterprise.deployment.EntityManagerReferenceDescriptor) HashSet(java.util.HashSet)

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