Search in sources :

Example 1 with TransactionManagerHelper

use of com.sun.enterprise.transaction.TransactionManagerHelper 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 2 with TransactionManagerHelper

use of com.sun.enterprise.transaction.TransactionManagerHelper 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

TransactionManagerHelper (com.sun.enterprise.transaction.TransactionManagerHelper)2 AroundInvoke (javax.interceptor.AroundInvoke)2 TransactionManager (javax.transaction.TransactionManager)2 TransactionalException (javax.transaction.TransactionalException)2 Transaction (javax.transaction.Transaction)1