Search in sources :

Example 1 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class EntityContainer method ejbStore_If_No_Transaction.

public void ejbStore_If_No_Transaction(final ThreadContext callContext, final EntityBean bean) throws Exception {
    final Operation currentOp = callContext.getCurrentOperation();
    if (currentOp == Operation.BUSINESS) {
        final TransactionPolicy callerTxPolicy = callContext.getTransactionPolicy();
        if (callerTxPolicy != null && callerTxPolicy.isTransactionActive()) {
            return;
        }
        final BeanContext beanContext = callContext.getBeanContext();
        final TransactionPolicy txPolicy = beanContext.getTransactionPolicyFactory().createTransactionPolicy(TransactionType.Supports);
        try {
            // double check we don't have an active transaction
            if (!txPolicy.isTransactionActive()) {
                callContext.setCurrentOperation(Operation.STORE);
                bean.ejbStore();
            }
        } catch (final Exception e) {
            instanceManager.discardInstance(callContext, bean);
            throw e;
        } finally {
            callContext.setCurrentOperation(currentOp);
            txPolicy.commit();
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Operation(org.apache.openejb.core.Operation) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) EJBAccessException(javax.ejb.EJBAccessException) NoSuchEntityException(javax.ejb.NoSuchEntityException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(org.apache.openejb.SystemException)

Example 2 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class EntityInstanceManager method freeInstance.

public void freeInstance(final ThreadContext callContext, final EntityBean bean) throws SystemException {
    discardInstance(callContext, bean);
    final Operation currentOp = callContext.getCurrentOperation();
    callContext.setCurrentOperation(Operation.UNSET_CONTEXT);
    try {
        /*
            * unsetEntityContext 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.unsetEntityContext();
    } catch (final Exception e) {
        /*
            * The EJB 1.1 specification does not specify how exceptions thrown by unsetEntityContext impact the
            * transaction, if there is one.  In this case we choose to do nothing since the instance is being disposed
            * of anyway.
            */
        logger.info(getClass().getName() + ".freeInstance: ignoring exception " + e + " on bean instance " + bean);
    } finally {
        callContext.setCurrentOperation(currentOp);
    }
}
Also used : Operation(org.apache.openejb.core.Operation) 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)

Example 3 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class EjbTransactionUtil method handleSystemException.

/**
     * Performs EJB rules when a system exception occurs.
     */
public static void handleSystemException(final TransactionPolicy txPolicy, final Throwable sysException, final ThreadContext callContext) throws InvalidateReferenceException {
    // Log the system exception or error
    Operation operation = null;
    if (callContext != null) {
        operation = callContext.getCurrentOperation();
    }
    if (operation != null) {
        logger.error("EjbTransactionUtil.handleSystemException: " + sysException.getMessage(), sysException);
    } else {
        logger.debug("EjbTransactionUtil.handleSystemException: " + sysException.getMessage(), sysException);
    }
    // Mark the transaction for rollback
    txPolicy.setRollbackOnly(sysException);
    // Throw InvalidateReferenceException
    if (txPolicy.isClientTransaction()) {
        // using caller's transaction
        final String message = "The transaction has been marked rollback only because the bean encountered a non-application exception :" + sysException.getClass().getName() + " : " + sysException.getMessage();
        final TransactionRolledbackException txException = new TransactionRolledbackException(message, sysException);
        throw new InvalidateReferenceException(txException);
    } else {
        // no transaction or in a new transaction for this method call
        final RemoteException re = new RemoteException("The bean encountered a non-application exception", sysException);
        throw new InvalidateReferenceException(re);
    }
}
Also used : InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) Operation(org.apache.openejb.core.Operation) RemoteException(java.rmi.RemoteException)

Example 4 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class StatelessContainer method _invoke.

@SuppressWarnings("ThrowFromFinallyBlock")
private Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
    Object returnValue = null;
    try {
        if (type == InterfaceType.SERVICE_ENDPOINT) {
            callContext.setCurrentOperation(Operation.BUSINESS_WS);
            returnValue = invokeWebService(args, beanContext, runMethod, instance);
        } else {
            final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
            final Operation operation = type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS;
            final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, operation, interceptors, instance.interceptors);
            returnValue = interceptorStack.invoke(args);
        }
    } catch (final Throwable re) {
        // handle reflection exception
        final ExceptionType exceptionType = beanContext.getExceptionType(re);
        if (exceptionType == ExceptionType.SYSTEM) {
            /* System Exception ****************************/
            // The bean instance is not put into the pool via instanceManager.poolInstance
            // and therefore the instance will be garbage collected and destroyed.
            // In case of StrictPooling flag being set to true we also release the semaphore
            // in the discardInstance method of the instanceManager.
            callContext.setDiscardInstance(true);
            handleSystemException(txPolicy, re, callContext);
        } else {
            /* Application Exception ***********************/
            handleApplicationException(txPolicy, re, exceptionType == ExceptionType.APPLICATION_ROLLBACK);
        }
    } finally {
        try {
            afterInvoke(txPolicy, callContext);
        } catch (final SystemException | RuntimeException e) {
            callContext.setDiscardInstance(true);
            throw e;
        }
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemException(org.apache.openejb.SystemException) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Operation(org.apache.openejb.core.Operation)

Example 5 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class MdbInstanceFactory method freeInstance.

/**
     * Frees an instance no longer needed by the resource adapter.  This method makes all the necessary lifecycle
     * callbacks and decrements the instance count.  This method should not be used to disposed of beans that have
     * thrown a system exception.  Instead the discardInstance method should be called.
     *
     * @param instance             the bean instance to free
     * @param ignoredInstanceCount
     */
public void freeInstance(final Instance instance, final boolean ignoredInstanceCount) {
    if (instance == null) {
        throw new NullPointerException("bean is null");
    }
    // decrement the instance count
    if (!ignoredInstanceCount) {
        synchronized (this) {
            instanceCount--;
        }
    }
    final ThreadContext callContext = ThreadContext.getThreadContext();
    final Operation originalOperation = callContext == null ? null : callContext.getCurrentOperation();
    final BaseContext.State[] originalAllowedStates = callContext == null ? null : callContext.getCurrentAllowedStates();
    try {
        // call post destroy method
        if (callContext != null) {
            callContext.setCurrentOperation(Operation.PRE_DESTROY);
        }
        final Method remove = instance.bean instanceof MessageDrivenBean ? MessageDrivenBean.class.getMethod("ejbRemove") : null;
        final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
        final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
        interceptorStack.invoke();
        if (instance.creationalContext != null) {
            instance.creationalContext.release();
        }
    } catch (final Throwable re) {
        MdbInstanceFactory.logger.error("The bean instance " + instance.bean + " threw a system exception:" + re, re);
    } finally {
        if (callContext != null) {
            callContext.setCurrentOperation(originalOperation);
            callContext.setCurrentAllowedStates(originalAllowedStates);
        }
    }
}
Also used : MessageDrivenBean(javax.ejb.MessageDrivenBean) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) ThreadContext(org.apache.openejb.core.ThreadContext) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method)

Aggregations

Operation (org.apache.openejb.core.Operation)12 ApplicationException (org.apache.openejb.ApplicationException)9 SystemException (org.apache.openejb.SystemException)8 OpenEJBException (org.apache.openejb.OpenEJBException)7 RemoteException (java.rmi.RemoteException)6 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)6 NoSuchEntityException (javax.ejb.NoSuchEntityException)5 BeanContext (org.apache.openejb.BeanContext)5 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)5 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)4 TransactionRolledbackException (org.apache.openejb.core.transaction.TransactionRolledbackException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 NoSuchObjectException (java.rmi.NoSuchObjectException)3 EJBAccessException (javax.ejb.EJBAccessException)3 NoSuchObjectException (org.apache.openejb.core.NoSuchObjectException)3 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)3 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)3 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)3 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)3