Search in sources :

Example 1 with TransactionalException

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

the class TransactionalInterceptorMandatory method transactional.

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

Example 2 with TransactionalException

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

the class TransactionalInterceptorNotSupported method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_NOTSUPPORTED);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(true);
    try {
        Transaction transaction = null;
        if (getTransactionManager().getTransaction() != null) {
            _logger.log(FINE, CDI_JTA_MBNOTSUPPORTED);
            try {
                transaction = getTransactionManager().suspend();
            } catch (Exception exception) {
                _logger.log(FINE, CDI_JTA_MBNOTSUPPORTEDTX, exception);
                throw new TransactionalException("Managed bean with Transactional annotation and TxType of NOT_SUPPORTED " + "called inside a transaction context.  Suspending transaction failed due to " + exception, exception);
            }
        }
        Object proceed = null;
        try {
            proceed = proceed(ctx);
        } finally {
            if (transaction != null) {
                try {
                    getTransactionManager().resume(transaction);
                } catch (Exception exception) {
                    throw new TransactionalException("Managed bean with Transactional annotation and TxType of NOT_SUPPORTED " + "encountered exception during resume " + exception, exception);
                }
            }
        }
        return proceed;
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : Transaction(javax.transaction.Transaction) TransactionalException(javax.transaction.TransactionalException) TransactionalException(javax.transaction.TransactionalException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 3 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 4 with TransactionalException

use of javax.transaction.TransactionalException in project crnk-framework by crnk-project.

the class CdiTransactionRunnerTest method testTransactionalRuntimeExceptionToBeUnwrapped.

@Test
public void testTransactionalRuntimeExceptionToBeUnwrapped() throws Exception {
    Callable callable = Mockito.mock(Callable.class);
    Mockito.when(callable.call()).thenThrow(new TransactionalException("a", new IllegalStateException("b")));
    try {
        runner.doInTransaction(callable);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertEquals("b", e.getMessage());
    }
    Mockito.verify(callable, Mockito.times(1)).call();
}
Also used : TransactionalException(javax.transaction.TransactionalException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 5 with TransactionalException

use of javax.transaction.TransactionalException in project narayana by jbosstm.

the class TransactionalImplTest method testClassLevelDefaultNeverExistingTX.

@Test
public void testClassLevelDefaultNeverExistingTX() throws Exception {
    TransactionManager tm = Utills.getTransactionManager();
    Utills.assertTransactionActive(false);
    tm.begin();
    Utills.assertTransactionActive(true);
    try {
        testTransactionalBean.invokeWithCLassLevelDefault();
        Assert.fail("Expected Exception to be thrown, but it was not");
    } catch (TransactionalException e) {
    // expected
    }
    Utills.assertTransactionActive(true);
    tm.commit();
    Utills.assertTransactionActive(false);
}
Also used : TransactionalException(javax.transaction.TransactionalException) TransactionManager(javax.transaction.TransactionManager) Test(org.junit.Test)

Aggregations

TransactionalException (javax.transaction.TransactionalException)13 AroundInvoke (javax.interceptor.AroundInvoke)5 Transaction (javax.transaction.Transaction)4 TransactionManager (javax.transaction.TransactionManager)4 Test (org.junit.Test)4 SystemException (javax.transaction.SystemException)3 TransactionManagerHelper (com.sun.enterprise.transaction.TransactionManagerHelper)2 Method (java.lang.reflect.Method)2 Callable (java.util.concurrent.Callable)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 TransactionRequiredException (javax.transaction.TransactionRequiredException)2 Transactional (javax.transaction.Transactional)2 IOException (java.io.IOException)1 InvalidTransactionException (javax.transaction.InvalidTransactionException)1 TransactionRolledbackException (javax.transaction.TransactionRolledbackException)1