Search in sources :

Example 1 with Stack

use of org.apache.openejb.util.Stack in project tomee by apache.

the class EntityInstanceManager method getPooledInstance.

protected EntityBean getPooledInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Stack methodReadyPool = poolMap.get(beanContext.getDeploymentID());
    if (methodReadyPool == null) {
        throw new SystemException("Invalid deployment id " + beanContext.getDeploymentID() + " for this container");
    }
    EntityBean bean = (EntityBean) methodReadyPool.pop();
    if (bean == null) {
        try {
            bean = (EntityBean) beanContext.getBeanClass().newInstance();
        } catch (final Exception e) {
            logger.error("Bean instantiation failed for class " + beanContext.getBeanClass(), e);
            throw new SystemException(e);
        }
        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.SET_CONTEXT);
        try {
            /*
                * setEntityContext executes in an unspecified transactional context. In this case we choose to
                * allow it to have what every transaction context is current. Better then suspending it
                * unnecessarily.
                *
                * We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
                * logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
                * we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
                * to do.
                */
            bean.setEntityContext(createEntityContext());
        } catch (final Exception e) {
            /*
                * The EJB 1.1 specification does not specify how exceptions thrown by setEntityContext impact the
                * transaction, if there is one.  In this case we choose the least disruptive operation, throwing an
                * application exception and NOT automatically marking the transaciton for rollback.
                */
            logger.error("Bean callback method failed ", e);
            throw new ApplicationException(e);
        } finally {
            callContext.setCurrentOperation(currentOp);
        }
    } else {
        reusingBean(bean, callContext);
    }
    if (callContext.getCurrentOperation() == Operation.BUSINESS || callContext.getCurrentOperation() == Operation.REMOVE) {
        /*
            * When a bean is retrieved from the bean pool to service a client's business method request it must be
            * notified that its about to enter service by invoking its ejbActivate( ) method. A bean instance
            * does not have its ejbActivate() invoked when:
            * 1. Its being retreived to service an ejbCreate()/ejbPostCreate().
            * 2. Its being retrieved to service an ejbFind method.
            * 3. Its being retrieved to service an ejbRemove() method.
            * See section 9.1.4 of the EJB 1.1 specification.
            */
        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.ACTIVATE);
        try {
            /*
                In the event of an exception, OpenEJB is required to log the exception, evict the instance,
                and mark the transaction for rollback.  If there is a transaction to rollback, then the a
                javax.transaction.TransactionRolledbackException must be throw to the client.
                See EJB 1.1 specification, section 12.3.2
                */
            bean.ejbActivate();
        } catch (final Throwable e) {
            logger.error("Encountered exception during call to ejbActivate()", e);
            final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
            if (txPolicy != null && txPolicy.isTransactionActive()) {
                txPolicy.setRollbackOnly(e);
                throw new ApplicationException(new TransactionRolledbackException("Reflection exception thrown while attempting to call ejbActivate() on the instance", e));
            }
            throw new ApplicationException(new RemoteException("Exception thrown while attempting to call ejbActivate() on the instance. Exception message = " + e.getMessage(), e));
        } finally {
            callContext.setCurrentOperation(currentOp);
        }
    }
    return bean;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) EntityBean(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Operation(org.apache.openejb.core.Operation) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) NoSuchObjectException(org.apache.openejb.core.NoSuchObjectException) RemoteException(java.rmi.RemoteException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) SystemException(org.apache.openejb.SystemException) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) ApplicationException(org.apache.openejb.ApplicationException) NoSuchEntityException(javax.ejb.NoSuchEntityException) LinkedListStack(org.apache.openejb.util.LinkedListStack) Stack(org.apache.openejb.util.Stack)

Example 2 with Stack

use of org.apache.openejb.util.Stack in project tomee by apache.

the class EntityInstanceManager method poolInstance.

public void poolInstance(final ThreadContext callContext, final EntityBean bean, final Object primaryKey) throws OpenEJBException {
    if (bean == null) {
        return;
    }
    // primary key is null if its a servicing a home methods (create, find, ejbHome)
    final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
    if (primaryKey != null && txPolicy != null && txPolicy.isTransactionActive()) {
        final Key key = new Key(callContext.getBeanContext().getDeploymentID(), primaryKey);
        SynchronizationWrapper wrapper = (SynchronizationWrapper) txPolicy.getResource(key);
        if (wrapper != null) {
            if (callContext.getCurrentOperation() == Operation.REMOVE) {
                /*
                    * The bean is being returned to the pool after it has been removed. Its
                    * important at this point to mark the bean as disassociated to prevent
                    * it's ejbStore method from bean called (see SynchronizationWrapper.beforeCompletion() method)
                    * and that subsequent methods can not be invoked on the bean identity (see obtainInstance() method).
                    */
                wrapper.disassociate();
                /*
                    * If the bean has been removed then the bean instance is no longer needed and can return to the methodReadyPool
                    * to service another identity.
                    */
                final Stack methodReadyPool = poolMap.get(callContext.getBeanContext().getDeploymentID());
                methodReadyPool.push(bean);
            } else {
                if (callContext.getCurrentOperation() == Operation.CREATE) {
                    // Bean is being recreated (new-delete-new) so we need to reassociate it
                    wrapper.associate();
                }
                wrapper.setEntityBean(bean);
            }
        } else {
            /*
                A wrapper will not exist if the bean is being returned after a create operation.
                In this case the transaction scope is broader then the create method itself; its a client
                initiated transaction, so the bean must be registered with the tranaction and moved to the
                tx ready pool
                */
            wrapper = new SynchronizationWrapper(callContext.getBeanContext(), primaryKey, bean, true, key, txPolicy);
            txPolicy.registerSynchronization(wrapper);
            txPolicy.putResource(key, wrapper);
        }
    } else {
        if (primaryKey != null && callContext.getCurrentOperation() != Operation.REMOVE) {
            /*
                * If the bean has a primary key; And its not being returned following a remove operation;
                * then the bean is being returned to the method ready pool after successfully executing a business method or create
                * method. In this case we need to call the bean instance's ejbPassivate before returning it to the pool per EJB 1.1
                * Section 9.1.
                */
            final Operation currentOp = callContext.getCurrentOperation();
            callContext.setCurrentOperation(Operation.PASSIVATE);
            try {
                /*
                    In the event of an exception, OpenEJB is required to log the exception, evict the instance,
                    and mark the transaction for rollback.  If there is a transaction to rollback, then the a
                    javax.transaction.TransactionRolledbackException must be throw to the client.
                    See EJB 1.1 specification, section 12.3.2
                    */
                bean.ejbPassivate();
            } catch (final Throwable e) {
                if (txPolicy.isTransactionActive()) {
                    txPolicy.setRollbackOnly(e);
                    throw new ApplicationException(new TransactionRolledbackException("Reflection exception thrown while attempting to call ejbPassivate() on the instance", e));
                }
                throw new ApplicationException(new RemoteException("Reflection exception thrown while attempting to call ejbPassivate() on the instance. Exception message = " + e.getMessage(), e));
            } finally {
                callContext.setCurrentOperation(currentOp);
            }
        }
        /*
            * The bean is returned to the method ready pool if its returned after servicing a find, ejbHome, business or create
            * method and is not still part of a tx.  While in the method ready pool the bean instance is not associated with a
            * primary key and may be used to service a request for any bean of the same class.
            */
        final Stack methodReadyPool = poolMap.get(callContext.getBeanContext().getDeploymentID());
        methodReadyPool.push(bean);
    }
}
Also used : ApplicationException(org.apache.openejb.ApplicationException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Operation(org.apache.openejb.core.Operation) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) RemoteException(java.rmi.RemoteException) LinkedListStack(org.apache.openejb.util.LinkedListStack) Stack(org.apache.openejb.util.Stack)

Aggregations

RemoteException (java.rmi.RemoteException)2 ApplicationException (org.apache.openejb.ApplicationException)2 Operation (org.apache.openejb.core.Operation)2 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)2 TransactionRolledbackException (org.apache.openejb.core.transaction.TransactionRolledbackException)2 LinkedListStack (org.apache.openejb.util.LinkedListStack)2 Stack (org.apache.openejb.util.Stack)2 EntityBean (javax.ejb.EntityBean)1 NoSuchEntityException (javax.ejb.NoSuchEntityException)1 BeanContext (org.apache.openejb.BeanContext)1 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 SystemException (org.apache.openejb.SystemException)1 NoSuchObjectException (org.apache.openejb.core.NoSuchObjectException)1