Search in sources :

Example 6 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testTransactionCommitWithRollbackException.

@Test
public void testTransactionCommitWithRollbackException() {
    given(manager.getTransaction()).willReturn(tx);
    given(tx.getRollbackOnly()).willReturn(true);
    willThrow(new RollbackException()).given(tx).commit();
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    try {
        Object result = tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                return l;
            }
        });
        assertSame(l, result);
    } catch (TransactionSystemException tse) {
        // expected
        assertTrue(tse.getCause() instanceof RollbackException);
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 7 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testParticipatingTransactionWithRollbackOnly.

@Test
public void testParticipatingTransactionWithRollbackOnly() {
    given(manager.getTransaction()).willReturn(tx);
    given(tx.isActive()).willReturn(true);
    given(tx.getRollbackOnly()).willReturn(true);
    willThrow(new RollbackException()).given(tx).commit();
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                return tt.execute(new TransactionCallback() {

                    @Override
                    public Object doInTransaction(TransactionStatus status) {
                        EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                        status.setRollbackOnly();
                        return null;
                    }
                });
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException tse) {
        // expected
        assertTrue(tse.getCause() instanceof RollbackException);
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(tx).setRollbackOnly();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 8 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project spring-framework by spring-projects.

the class CciLocalTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction();
    Connection con = txObject.getConnectionHolder().getConnection();
    if (status.isDebug()) {
        logger.debug("Committing CCI local transaction on Connection [" + con + "]");
    }
    try {
        con.getLocalTransaction().commit();
    } catch (LocalTransactionException ex) {
        throw new TransactionSystemException("Could not commit CCI local transaction", ex);
    } catch (ResourceException ex) {
        throw new TransactionSystemException("Unexpected failure on commit of CCI local transaction", ex);
    }
}
Also used : LocalTransactionException(javax.resource.spi.LocalTransactionException) Connection(javax.resource.cci.Connection) ResourceException(javax.resource.ResourceException) TransactionSystemException(org.springframework.transaction.TransactionSystemException)

Example 9 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project spring-framework by spring-projects.

the class CciLocalTransactionManager method doRollback.

@Override
protected void doRollback(DefaultTransactionStatus status) {
    CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction();
    Connection con = txObject.getConnectionHolder().getConnection();
    if (status.isDebug()) {
        logger.debug("Rolling back CCI local transaction on Connection [" + con + "]");
    }
    try {
        con.getLocalTransaction().rollback();
    } catch (LocalTransactionException ex) {
        throw new TransactionSystemException("Could not roll back CCI local transaction", ex);
    } catch (ResourceException ex) {
        throw new TransactionSystemException("Unexpected failure on rollback of CCI local transaction", ex);
    }
}
Also used : LocalTransactionException(javax.resource.spi.LocalTransactionException) Connection(javax.resource.cci.Connection) ResourceException(javax.resource.ResourceException) TransactionSystemException(org.springframework.transaction.TransactionSystemException)

Example 10 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project spring-framework by spring-projects.

the class JtaTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JtaTransactionObject txObject = (JtaTransactionObject) status.getTransaction();
    try {
        int jtaStatus = txObject.getUserTransaction().getStatus();
        if (jtaStatus == Status.STATUS_NO_TRANSACTION) {
            // In any case, the transaction is already fully cleaned up.
            throw new UnexpectedRollbackException("JTA transaction already completed - probably rolled back");
        }
        if (jtaStatus == Status.STATUS_ROLLEDBACK) {
            // IllegalStateException expected on JBoss; call still necessary.
            try {
                txObject.getUserTransaction().rollback();
            } catch (IllegalStateException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Rollback failure with transaction already marked as rolled back: " + ex);
                }
            }
            throw new UnexpectedRollbackException("JTA transaction already rolled back (probably due to a timeout)");
        }
        txObject.getUserTransaction().commit();
    } catch (RollbackException ex) {
        throw new UnexpectedRollbackException("JTA transaction unexpectedly rolled back (maybe due to a timeout)", ex);
    } catch (HeuristicMixedException ex) {
        throw new HeuristicCompletionException(HeuristicCompletionException.STATE_MIXED, ex);
    } catch (HeuristicRollbackException ex) {
        throw new HeuristicCompletionException(HeuristicCompletionException.STATE_ROLLED_BACK, ex);
    } catch (IllegalStateException ex) {
        throw new TransactionSystemException("Unexpected internal transaction state", ex);
    } catch (SystemException ex) {
        throw new TransactionSystemException("JTA failure on commit", ex);
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Aggregations

TransactionSystemException (org.springframework.transaction.TransactionSystemException)27 SystemException (javax.transaction.SystemException)7 Test (org.junit.Test)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 InvalidTransactionException (javax.transaction.InvalidTransactionException)6 NotSupportedException (javax.transaction.NotSupportedException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 SQLException (java.sql.SQLException)5 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)3 RollbackException (javax.persistence.RollbackException)3 Connection (javax.resource.cci.Connection)3 LocalTransactionException (javax.resource.spi.LocalTransactionException)3 RollbackException (javax.transaction.RollbackException)3 UserTransaction (javax.transaction.UserTransaction)3 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)3 UOWException (com.ibm.wsspi.uow.UOWException)2 Connection (java.sql.Connection)2 JMSException (javax.jms.JMSException)2 Session (javax.jms.Session)2