FileDocCategorySizeDatePackage
EntityManagerWrapper.javaAPI DocGlassfish v2 API25172Fri May 04 22:35:58 BST 2007com.sun.enterprise.util

EntityManagerWrapper

public class EntityManagerWrapper extends Object implements EntityManager, Serializable
Implementation of a container-managed entity manager. A new instance of this class will be created for each injected EntityManager reference or each lookup of an EntityManager reference within the component jndi environment. The underlying EntityManager object does not support concurrent access. Likewise, this wrapper does not support concurrent access.
author
Kenneth Saks

Fields Summary
static Logger
_logger
private static LocalStringManagerImpl
localStrings
private String
unitName
private PersistenceContextType
contextType
private Map
emProperties
private transient EntityManagerFactory
entityManagerFactory
private transient com.sun.enterprise.J2EETransactionManager
txManager
private transient com.sun.ejb.ContainerFactory
containerFactory
private transient EntityManager
extendedEntityManager
private transient EntityManager
nonTxEntityManager
Constructors Summary
public EntityManagerWrapper(com.sun.enterprise.deployment.types.EntityManagerReference referenceDescriptor)

    
      
                                 
        this.unitName = referenceDescriptor.getUnitName();
        this.contextType = referenceDescriptor.getPersistenceContextType();
        this.emProperties = referenceDescriptor.getProperties();
    
Methods Summary
private javax.persistence.EntityManager_getDelegate()


        // Populate any transient objects the first time 
        // this method is called.

        if( entityManagerFactory == null ) {
            init();
        }

        EntityManager delegate = null;

        if( nonTxEntityManager != null ) {
            cleanupNonTxEntityManager();
        }

        if( contextType == PersistenceContextType.TRANSACTION ) {

            J2EETransaction tx = null;
            try {
                tx = (J2EETransaction) txManager.getTransaction();
            } catch(Exception e) {
                throw new IllegalStateException("exception retrieving tx", e);
            }
            
            if( tx != null ) {

                // If there is an active extended persistence context
                // for the same entity manager factory and the same tx,
                // it takes precendence.
                delegate = tx.getExtendedEntityManager(entityManagerFactory);

                if( delegate == null ) {

                    delegate = tx.getTxEntityManager(entityManagerFactory);

                    if( delegate == 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.
                        delegate = entityManagerFactory.
                            createEntityManager(emProperties);

                        tx.addTxEntityManagerMapping(entityManagerFactory, 
                                                     delegate);
                    }
                }

            } else {

                nonTxEntityManager = entityManagerFactory.createEntityManager
                    (emProperties);

                // Return a new non-transactional entity manager.
                delegate = nonTxEntityManager;
                    
            }

        } else {

            // EXTENDED Persitence Context

            if( extendedEntityManager == null ) {
                extendedEntityManager = containerFactory.
                    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;

    
private voidcleanupNonTxEntityManager()

        if( nonTxEntityManager != null ) {
            nonTxEntityManager.close();
            nonTxEntityManager = null;
        }
    
public voidclear()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CLEAR);
            }
            _getDelegate().clear();
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
    
public voidclose()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        if(callFlowAgent.isEnabled()) {
            callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CLOSE);
            callFlowAgent.entityManagerMethodEnd();
        }
        // close() not allowed on container-managed EMs.
        throw new IllegalStateException();
    
public booleancontains(java.lang.Object entity)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CONTAINS);
            }
            EntityManager delegate = _getDelegate();
            return delegate.contains(entity);
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
    
public javax.persistence.QuerycreateNamedQuery(java.lang.String name)

        Query returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CREATE_NAMED_QUERY);
            }
            EntityManager delegate = _getDelegate();
            returnValue = delegate.createNamedQuery(name);

            if( nonTxEntityManager != null ) {
                Query queryDelegate = returnValue;
                returnValue = QueryWrapper.createNamedQueryWrapper
                    (entityManagerFactory, emProperties, delegate,
                     queryDelegate, name);
                // It's now the responsibility of the QueryWrapper to 
                // close the non-tx EM delegate
                nonTxEntityManager = null;
            }
        } catch(RuntimeException re) {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            throw re;
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }

        return returnValue;
    
public javax.persistence.QuerycreateNativeQuery(java.lang.String sqlString)

        Query returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CREATE_NATIVE_QUERY_STRING);
            }
            EntityManager delegate = _getDelegate();
            returnValue = delegate.createNativeQuery(sqlString);

            if( nonTxEntityManager != null ) {
                Query queryDelegate = returnValue;
                returnValue = QueryWrapper.createNativeQueryWrapper
                    (entityManagerFactory, emProperties, delegate,
                     queryDelegate, sqlString);
                // It's now the responsibility of the QueryWrapper to 
                // close the non-tx EM delegate
                nonTxEntityManager = null;
            }
        } catch(RuntimeException re) {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            throw re;
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        return returnValue;
    
public javax.persistence.QuerycreateNativeQuery(java.lang.String sqlString, java.lang.Class resultClass)

        Query returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CREATE_NATIVE_QUERY_STRING_CLASS);
            }
            EntityManager delegate = _getDelegate();
            returnValue = delegate.createNativeQuery(sqlString, resultClass);

            if( nonTxEntityManager != null ) {
                Query queryDelegate = returnValue;
                returnValue = QueryWrapper.createNativeQueryWrapper
                    (entityManagerFactory, emProperties, delegate,
                     queryDelegate, sqlString, resultClass);
                // It's now the responsibility of the QueryWrapper to 
                // close the non-tx EM delegate
                nonTxEntityManager = null;
            }
        } catch(RuntimeException re) {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            throw re;
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        return returnValue;
    
public javax.persistence.QuerycreateNativeQuery(java.lang.String sqlString, java.lang.String resultSetMapping)

        Query returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CREATE_NATIVE_QUERY_STRING_STRING);
            }
            EntityManager delegate = _getDelegate();
            returnValue = delegate.createNativeQuery
                (sqlString, resultSetMapping);

            if( nonTxEntityManager != null ) {
                Query queryDelegate = returnValue;
                returnValue = QueryWrapper.createNativeQueryWrapper
                    (entityManagerFactory, emProperties, delegate,
                     queryDelegate, sqlString, resultSetMapping);
                // It's now the responsibility of the QueryWrapper to 
                // close the non-tx EM delegate
                nonTxEntityManager = null;
            }
        } catch(RuntimeException re) {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            throw re;
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        return returnValue;
    
public javax.persistence.QuerycreateQuery(java.lang.String ejbqlString)

        Query returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.CREATE_QUERY);
            }
            EntityManager delegate = _getDelegate();
            returnValue = delegate.createQuery(ejbqlString);

            if( nonTxEntityManager != null ) {
                Query queryDelegate = returnValue;
                returnValue = QueryWrapper.createQueryWrapper
                    (entityManagerFactory, emProperties, delegate,
                     queryDelegate, ejbqlString);
                // It's now the responsibility of the QueryWrapper to 
                // close the non-tx EM delegate
                nonTxEntityManager = null;
            }
        } catch(RuntimeException re) {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            throw re;
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        return returnValue;
    
private voiddoTransactionScopedTxCheck()

        
        if( contextType != PersistenceContextType.TRANSACTION) {
            return;
        }
        
        doTxRequiredCheck();

    
private voiddoTxRequiredCheck()


        if( entityManagerFactory == null ) {
            init();
        }

        J2EETransaction tx = null;
        try {
            tx = (J2EETransaction) txManager.getTransaction();
        } catch(Exception e) {
            throw new IllegalStateException("exception retrieving tx", e);
        }
            
        if( tx == null ) {
            throw new TransactionRequiredException();
        }

    
public Tfind(java.lang.Class entityClass, java.lang.Object primaryKey)

        T returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.FIND);
            }
            returnValue = _getDelegate().find(entityClass, primaryKey);
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        return returnValue;
    
public voidflush()

        // tx is ALWAYS required, regardless of persistence context type.
        doTxRequiredCheck();
        
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.FLUSH);
            }
            _getDelegate().flush();
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        
        // tx is required so there's no need to do any non-tx cleanup
    
public java.lang.ObjectgetDelegate()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.GET_DELEGATE);
            }
            return _getDelegate();
        } finally {
            if( nonTxEntityManager != null ) {
                // In this case we can't close the physical EntityManager
                // before returning it to the application, so we just clear
                // the EM wrapper's reference to it.  
                nonTxEntityManager = null;
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }

    
public javax.persistence.FlushModeTypegetFlushMode()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.GET_FLUSH_MODE);
            }
            return _getDelegate().getFlushMode();
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
    
public TgetReference(java.lang.Class entityClass, java.lang.Object primaryKey)

        T returnValue = null;
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.GET_REFERENCE);
            }
            returnValue = _getDelegate().getReference(entityClass, primaryKey);
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        return returnValue;
    
public javax.persistence.EntityTransactiongetTransaction()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.GET_TRANSACTION);
            }
            return _getDelegate().getTransaction();
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
    
private voidinit()


        entityManagerFactory = EntityManagerFactoryWrapper.
            lookupEntityManagerFactory(unitName);
        
        if( entityManagerFactory == null ) {
            throw new IllegalStateException
                ("Unable to retrieve EntityManagerFactory for unitName "
                 + unitName);
        }
        
        txManager = Switch.getSwitch().getTransactionManager();
        containerFactory = Switch.getSwitch().getContainerFactory();

    
public booleanisOpen()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        if(callFlowAgent.isEnabled()) {
            callFlowAgent.entityManagerMethodStart(EntityManagerMethod.IS_OPEN);
            callFlowAgent.entityManagerMethodEnd();
        }
        // Not relevant for container-managed EMs.  Just return true.
        return true;
    
public voidjoinTransaction()

        // Doesn't apply to the container-managed case, but all the
        // spec says is that an exception should be thrown if called
        // without a tx.
        doTxRequiredCheck();
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        if(callFlowAgent.isEnabled()) {
            callFlowAgent.entityManagerMethodStart(EntityManagerMethod.JOIN_TRANSACTION);
            callFlowAgent.entityManagerMethodEnd();
        }

        // There's no point in calling anything on the physical 
        // entity manager since in all tx cases it will be
        // correctly associated with a tx already.
    
public voidlock(java.lang.Object entity, javax.persistence.LockModeType lockMode)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.LOCK);
            }
            _getDelegate().lock(entity, lockMode);
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
    
public Tmerge(T entity)

        doTransactionScopedTxCheck();
        
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.MERGE);
            }
            return _getDelegate().merge(entity);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }

        // tx is required so there's no need to do any non-tx cleanup
    
public voidpersist(java.lang.Object entity)

        doTransactionScopedTxCheck();
        
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.PERSIST);
            }
            _getDelegate().persist(entity);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }

        // tx is required so there's no need to do any non-tx cleanup
    
public voidrefresh(java.lang.Object entity)

        doTransactionScopedTxCheck();
        
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.REFRESH);
            }
            _getDelegate().refresh(entity);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        
        // tx is required so there's no need to do any non-tx cleanup
    
public voidremove(java.lang.Object entity)

        doTransactionScopedTxCheck();
        
        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.REMOVE);
            }
            _getDelegate().remove(entity);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }
        
        // tx is required so there's no need to do any non-tx cleanup
    
public voidsetFlushMode(javax.persistence.FlushModeType flushMode)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodStart(EntityManagerMethod.SET_FLUSH_MODE);
            }
            _getDelegate().setFlushMode(flushMode);
        } finally {
            if( nonTxEntityManager != null ) {
                cleanupNonTxEntityManager();
            }
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerMethodEnd();
            }
        }