Search in sources :

Example 6 with TransactionalException

use of javax.transaction.TransactionalException in project Payara by payara.

the class TransactionalInterceptorRequired method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_REQUIRED);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(false);
    try {
        boolean isTransactionStarted = false;
        if (getTransactionManager().getTransaction() == null) {
            _logger.log(FINE, CDI_JTA_MBREQUIRED);
            try {
                getTransactionManager().begin();
                TransactionManager transactionManager = getTransactionManager();
                if (transactionManager instanceof TransactionManagerHelper) {
                    ((TransactionManagerHelper) transactionManager).preInvokeTx(true);
                }
            } catch (Exception exception) {
                _logger.log(FINE, CDI_JTA_MBREQUIREDBT, exception);
                throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during begin " + exception, exception);
            }
            isTransactionStarted = true;
        }
        Object proceed = null;
        try {
            proceed = proceed(ctx);
        } finally {
            if (isTransactionStarted) {
                try {
                    TransactionManager transactionManager = getTransactionManager();
                    if (transactionManager instanceof TransactionManagerHelper) {
                        ((TransactionManagerHelper) transactionManager).postInvokeTx(false, true);
                    }
                    // Exception handling for proceed method call above can set TM/TRX as setRollbackOnly
                    if (getTransactionManager().getTransaction().getStatus() == STATUS_MARKED_ROLLBACK) {
                        getTransactionManager().rollback();
                    } else {
                        getTransactionManager().commit();
                    }
                } catch (Exception exception) {
                    _logger.log(FINE, CDI_JTA_MBREQUIREDCT, exception);
                    throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during commit " + exception, exception);
                }
            }
        }
        return proceed;
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : TransactionManagerHelper(com.sun.enterprise.transaction.TransactionManagerHelper) TransactionalException(javax.transaction.TransactionalException) TransactionManager(javax.transaction.TransactionManager) TransactionalException(javax.transaction.TransactionalException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 7 with TransactionalException

use of javax.transaction.TransactionalException in project tomee by apache.

the class InterceptorBase method intercept.

protected Object intercept(final InvocationContext ic) throws Exception {
    TransactionPolicy policy = null;
    final boolean forbidsUt = doesForbidUtUsage();
    final RuntimeException oldEx;
    final IllegalStateException illegalStateException;
    if (forbidsUt) {
        illegalStateException = ILLEGAL_STATE_EXCEPTION;
        oldEx = CoreUserTransaction.error(illegalStateException);
    } else {
        illegalStateException = null;
        oldEx = null;
    }
    try {
        policy = getPolicy();
        final Object proceed = ic.proceed();
        // force commit there to ensure we can catch synchro exceptions
        policy.commit();
        return proceed;
    } catch (final Exception e) {
        if (illegalStateException == e) {
            throw e;
        }
        Exception error = unwrap(e);
        if (error != null && (!HANDLE_EXCEPTION_ONLY_FOR_CLIENT || policy.isNewTransaction())) {
            final Method method = ic.getMethod();
            if (rollback == null) {
                synchronized (this) {
                    if (rollback == null) {
                        rollback = new ConcurrentHashMap<>();
                    }
                }
            }
            Boolean doRollback = rollback.get(method);
            if (doRollback != null) {
                if (doRollback && policy != null && policy.isTransactionActive()) {
                    policy.setRollbackOnly();
                }
            } else {
                // computed lazily but we could cache it later for sure if that's really a normal case
                final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(method.getDeclaringClass());
                Transactional tx = null;
                for (final AnnotatedMethod<?> m : annotatedType.getMethods()) {
                    if (method.equals(m.getJavaMember())) {
                        tx = m.getAnnotation(Transactional.class);
                        break;
                    }
                }
                if (tx == null) {
                    tx = annotatedType.getAnnotation(Transactional.class);
                }
                if (tx != null) {
                    doRollback = new ExceptionPriotiryRules(tx.rollbackOn(), tx.dontRollbackOn()).accept(error, method.getExceptionTypes());
                    rollback.putIfAbsent(method, doRollback);
                    if (doRollback && policy != null && policy.isTransactionActive()) {
                        policy.setRollbackOnly();
                    }
                }
            }
        }
        if (policy != null) {
            try {
                policy.commit();
            } catch (final Exception ex) {
                // no-op: swallow to keep the right exception
                final Logger logger = Logger.getLogger(getClass().getName());
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Swallowing: " + ex.getMessage());
                }
            }
        }
        if (error == null || TransactionRequiredException.class.isInstance(error)) {
            throw new TransactionalException(e.getMessage(), error);
        }
        throw error;
    } finally {
        if (forbidsUt) {
            CoreUserTransaction.resetError(oldEx);
        }
    }
}
Also used : AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) TransactionalException(javax.transaction.TransactionalException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Logger(java.util.logging.Logger) TransactionRequiredException(javax.transaction.TransactionRequiredException) TransactionalException(javax.transaction.TransactionalException) SystemException(org.apache.openejb.SystemException) RollbackException(javax.transaction.RollbackException) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) ApplicationException(org.apache.openejb.ApplicationException) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Transactional(javax.transaction.Transactional)

Example 8 with TransactionalException

use of javax.transaction.TransactionalException in project Payara by payara.

the class TransactionalInterceptorNever method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_NEVER);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(true);
    try {
        if (getTransactionManager().getTransaction() != null) {
            throw new TransactionalException("InvalidTransactionException thrown from TxType.NEVER transactional interceptor.", new InvalidTransactionException("Managed bean with Transactional annotation and TxType of NEVER " + "called inside a transaction context"));
        }
        return proceed(ctx);
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : TransactionalException(javax.transaction.TransactionalException) InvalidTransactionException(javax.transaction.InvalidTransactionException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 9 with TransactionalException

use of javax.transaction.TransactionalException in project Payara by payara.

the class TransactionalInterceptorRequiresNew method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_REQNEW);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(false);
    try {
        Transaction suspendedTransaction = null;
        if (getTransactionManager().getTransaction() != null) {
            _logger.log(FINE, CDI_JTA_MBREQNEW);
            suspendedTransaction = getTransactionManager().suspend();
        // todo catch, wrap in new transactional exception and throw
        }
        try {
            getTransactionManager().begin();
            TransactionManager tm = getTransactionManager();
            if (tm instanceof TransactionManagerHelper) {
                ((TransactionManagerHelper) tm).preInvokeTx(true);
            }
        } catch (Exception exception) {
            _logger.log(FINE, CDI_JTA_MBREQNEWBT, exception);
            throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRES_NEW " + "encountered exception during begin " + exception, exception);
        }
        Object proceed = null;
        try {
            proceed = proceed(ctx);
        } finally {
            try {
                TransactionManager tm = getTransactionManager();
                if (tm instanceof TransactionManagerHelper) {
                    ((TransactionManagerHelper) tm).postInvokeTx(false, true);
                }
                // Exception handling for proceed method call above can set TM/TRX as setRollbackOnly
                if (getTransactionManager().getTransaction().getStatus() == STATUS_MARKED_ROLLBACK) {
                    getTransactionManager().rollback();
                } else {
                    getTransactionManager().commit();
                }
            } catch (Exception exception) {
                _logger.log(FINE, CDI_JTA_MBREQNEWCT, exception);
                throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRES_NEW " + "encountered exception during commit " + exception, exception);
            }
            if (suspendedTransaction != null) {
                try {
                    getTransactionManager().resume(suspendedTransaction);
                } catch (Exception exception) {
                    throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during resume " + exception, exception);
                }
            }
        }
        return proceed;
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : TransactionManagerHelper(com.sun.enterprise.transaction.TransactionManagerHelper) Transaction(javax.transaction.Transaction) TransactionalException(javax.transaction.TransactionalException) TransactionManager(javax.transaction.TransactionManager) TransactionalException(javax.transaction.TransactionalException) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

TransactionalException (javax.transaction.TransactionalException)9 AroundInvoke (javax.interceptor.AroundInvoke)5 Transaction (javax.transaction.Transaction)4 SystemException (javax.transaction.SystemException)3 TransactionManagerHelper (com.sun.enterprise.transaction.TransactionManagerHelper)2 Method (java.lang.reflect.Method)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Logger (java.util.logging.Logger)2 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)2 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)2 NotSupportedException (javax.transaction.NotSupportedException)2 RollbackException (javax.transaction.RollbackException)2 TransactionManager (javax.transaction.TransactionManager)2 TransactionRequiredException (javax.transaction.TransactionRequiredException)2 Transactional (javax.transaction.Transactional)2 InvalidTransactionException (javax.transaction.InvalidTransactionException)1 TransactionRolledbackException (javax.transaction.TransactionRolledbackException)1 ApplicationException (org.apache.openejb.ApplicationException)1 SystemException (org.apache.openejb.SystemException)1 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)1