Search in sources :

Example 31 with RollbackException

use of javax.transaction.RollbackException in project spring-framework by spring-projects.

the class JtaTransactionManager method registerAfterCompletionWithExistingTransaction.

@Override
protected void registerAfterCompletionWithExistingTransaction(Object transaction, List<TransactionSynchronization> synchronizations) {
    JtaTransactionObject txObject = (JtaTransactionObject) transaction;
    logger.debug("Registering after-completion synchronization with existing JTA transaction");
    try {
        doRegisterAfterCompletionWithJtaTransaction(txObject, synchronizations);
    } catch (SystemException ex) {
        throw new TransactionSystemException("JTA failure on registerSynchronization", ex);
    } catch (Exception ex) {
        // Note: JBoss throws plain RuntimeException with RollbackException as cause.
        if (ex instanceof RollbackException || ex.getCause() instanceof RollbackException) {
            logger.debug("Participating in existing JTA transaction that has been marked for rollback: " + "cannot register Spring after-completion callbacks with outer JTA transaction - " + "immediately performing Spring after-completion callbacks with outcome status 'rollback'. " + "Original exception: " + ex);
            invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
        } else {
            logger.debug("Participating in existing JTA transaction, but unexpected internal transaction " + "state encountered: cannot register Spring after-completion callbacks with outer JTA " + "transaction - processing Spring after-completion callbacks with outcome status 'unknown'" + "Original exception: " + ex);
            invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
        }
    }
}
Also used : TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(javax.transaction.SystemException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) NamingException(javax.naming.NamingException) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.transaction.RollbackException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) TransactionSuspensionNotSupportedException(org.springframework.transaction.TransactionSuspensionNotSupportedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) IOException(java.io.IOException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 32 with RollbackException

use of javax.transaction.RollbackException in project spring-framework by spring-projects.

the class WebSphereUowTransactionManagerTests method newTransactionWithCommitException.

@Test
public void newTransactionWithCommitException() {
    final RollbackException rex = new RollbackException();
    MockUOWManager manager = new MockUOWManager() {

        @Override
        public void runUnderUOW(int type, boolean join, UOWAction action) throws UOWException {
            throw new UOWException(rex);
        }
    };
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    try {
        ptm.execute(definition, new TransactionCallback<String>() {

            @Override
            public String doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                return "result";
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException ex) {
        // expected
        assertTrue(ex.getCause() instanceof UOWException);
        assertSame(rex, ex.getRootCause());
        assertSame(rex, ex.getMostSpecificCause());
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) UOWException(com.ibm.wsspi.uow.UOWException) TransactionStatus(org.springframework.transaction.TransactionStatus) UOWAction(com.ibm.wsspi.uow.UOWAction) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Example 33 with RollbackException

use of javax.transaction.RollbackException in project wildfly by wildfly.

the class BeforeCompletionExceptionDestroysBeanTestCase method testExceptionInBeforeCompletionDestroysBean.

@Test
public void testExceptionInBeforeCompletionDestroysBean() throws NamingException, SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException {
    final UserTransaction userTransaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    final BeforeCompletionSFSB bean = (BeforeCompletionSFSB) new InitialContext().lookup("java:module/" + BeforeCompletionSFSB.class.getSimpleName());
    //begin a transaction
    userTransaction.begin();
    bean.enlist();
    //commit, this should destroy the bean, as it's @BeforeCompletion method will throw an exception
    try {
        userTransaction.commit();
    } catch (RollbackException expected) {
    }
    try {
        userTransaction.begin();
        bean.enlist();
        throw new RuntimeException("Expected SFSB to be destroyed");
    } catch (NoSuchEJBException expected) {
    } finally {
        userTransaction.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) NoSuchEJBException(javax.ejb.NoSuchEJBException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 34 with RollbackException

use of javax.transaction.RollbackException in project Activiti by Activiti.

the class JtaTransactionContext method addTransactionListener.

public void addTransactionListener(TransactionState transactionState, final TransactionListener transactionListener) {
    Transaction transaction = getTransaction();
    CommandContext commandContext = Context.getCommandContext();
    try {
        transaction.registerSynchronization(new TransactionStateSynchronization(transactionState, transactionListener, commandContext));
    } catch (IllegalStateException e) {
        throw new ActivitiException("IllegalStateException while registering synchronization ", e);
    } catch (RollbackException e) {
        throw new ActivitiException("RollbackException while registering synchronization ", e);
    } catch (SystemException e) {
        throw new ActivitiException("SystemException while registering synchronization ", e);
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Transaction(javax.transaction.Transaction) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

Example 35 with RollbackException

use of javax.transaction.RollbackException in project galley by Commonjava.

the class FastLocalCacheProvider method copy.

@Override
public void copy(ConcreteResource from, ConcreteResource to) throws IOException {
    final String fromNFSPath = getKeyForResource(from);
    final String toNFSPath = getKeyForResource(to);
    //FIXME: there is no good solution here for thread locking as there are two resource needs to be locked. If handled not correctly, will cause dead lock
    InputStream nfsFrom = null;
    OutputStream nfsTo = null;
    try {
        //FIXME: need to think about this lock of the re-entrant way and ISPN lock wait
        nfsOwnerCache.beginTransaction();
        nfsOwnerCache.lock(fromNFSPath, toNFSPath);
        plCacheProvider.copy(from, to);
        nfsFrom = new FileInputStream(getNFSDetachedFile(from));
        File nfsToFile = getNFSDetachedFile(to);
        if (!nfsToFile.exists() && !nfsToFile.isDirectory()) {
            if (!nfsToFile.getParentFile().exists()) {
                nfsToFile.getParentFile().mkdirs();
            }
            try {
                nfsToFile.createNewFile();
            } catch (IOException e) {
                logger.error("[galley] New nfs file created not properly.", e);
            }
        }
        nfsTo = new FileOutputStream(nfsToFile);
        IOUtils.copy(nfsFrom, nfsTo);
        //FIXME: need to use put?
        nfsOwnerCache.putIfAbsent(toNFSPath, getCurrentNodeIp());
        nfsOwnerCache.commit();
    } catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
        logger.error("[galley] Transaction error for nfs cache during file copying.", e);
        try {
            nfsOwnerCache.rollback();
        } catch (SystemException se) {
            final String errorMsg = "[galley] Transaction rollback error for nfs cache during file copying.";
            logger.error(errorMsg, se);
            throw new IllegalStateException(errorMsg, se);
        }
    } finally {
        IOUtils.closeQuietly(nfsFrom);
        IOUtils.closeQuietly(nfsTo);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) FileInputStream(java.io.FileInputStream) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) FileOutputStream(java.io.FileOutputStream) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException) File(java.io.File)

Aggregations

RollbackException (javax.transaction.RollbackException)57 SystemException (javax.transaction.SystemException)25 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)15 Transaction (javax.transaction.Transaction)14 UserTransaction (javax.transaction.UserTransaction)14 HeuristicMixedException (javax.transaction.HeuristicMixedException)12 Test (org.junit.Test)11 Connection (java.sql.Connection)8 XAException (javax.transaction.xa.XAException)8 PreparedStatement (java.sql.PreparedStatement)7 NotSupportedException (javax.transaction.NotSupportedException)7 IOException (java.io.IOException)6 EJBException (javax.ejb.EJBException)6 Account (org.apache.openejb.test.object.Account)6 RemoteException (java.rmi.RemoteException)5 CreateException (javax.ejb.CreateException)5 DataSource (javax.sql.DataSource)5 SQLException (java.sql.SQLException)4 Synchronization (javax.transaction.Synchronization)4 XAResource (javax.transaction.xa.XAResource)4