Search in sources :

Example 11 with TransactionSystemException

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

the class WebLogicJtaTransactionManager method retrieveUserTransaction.

@Override
protected UserTransaction retrieveUserTransaction() throws TransactionSystemException {
    loadWebLogicTransactionHelper();
    try {
        logger.debug("Retrieving JTA UserTransaction from WebLogic TransactionHelper");
        Method getUserTransactionMethod = this.transactionHelper.getClass().getMethod("getUserTransaction");
        return (UserTransaction) getUserTransactionMethod.invoke(this.transactionHelper);
    } catch (InvocationTargetException ex) {
        throw new TransactionSystemException("WebLogic's TransactionHelper.getUserTransaction() method failed", ex.getTargetException());
    } catch (Exception ex) {
        throw new TransactionSystemException("Could not invoke WebLogic's TransactionHelper.getUserTransaction() method", ex);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Method(java.lang.reflect.Method) TransactionSystemException(org.springframework.transaction.TransactionSystemException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NotSupportedException(javax.transaction.NotSupportedException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(javax.transaction.SystemException) InvalidTransactionException(javax.transaction.InvalidTransactionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 12 with TransactionSystemException

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

the class WebSphereUowTransactionManager method execute.

@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
    if (definition == null) {
        // Use defaults if no transaction definition given.
        definition = new DefaultTransactionDefinition();
    }
    if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
        throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
    }
    int pb = definition.getPropagationBehavior();
    boolean existingTx = (this.uowManager.getUOWStatus() != UOWSynchronizationRegistry.UOW_STATUS_NONE && this.uowManager.getUOWType() != UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION);
    int uowType = UOWSynchronizationRegistry.UOW_TYPE_GLOBAL_TRANSACTION;
    boolean joinTx = false;
    boolean newSynch = false;
    if (existingTx) {
        if (pb == TransactionDefinition.PROPAGATION_NEVER) {
            throw new IllegalTransactionStateException("Transaction propagation 'never' but existing transaction found");
        }
        if (pb == TransactionDefinition.PROPAGATION_NESTED) {
            throw new NestedTransactionNotSupportedException("Transaction propagation 'nested' not supported for WebSphere UOW transactions");
        }
        if (pb == TransactionDefinition.PROPAGATION_SUPPORTS || pb == TransactionDefinition.PROPAGATION_REQUIRED || pb == TransactionDefinition.PROPAGATION_MANDATORY) {
            joinTx = true;
            newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        } else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
            uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
            newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
        } else {
            newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        }
    } else {
        if (pb == TransactionDefinition.PROPAGATION_MANDATORY) {
            throw new IllegalTransactionStateException("Transaction propagation 'mandatory' but no existing transaction found");
        }
        if (pb == TransactionDefinition.PROPAGATION_SUPPORTS || pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED || pb == TransactionDefinition.PROPAGATION_NEVER) {
            uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
            newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
        } else {
            newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        }
    }
    boolean debug = logger.isDebugEnabled();
    if (debug) {
        logger.debug("Creating new transaction with name [" + definition.getName() + "]: " + definition);
    }
    SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend(null) : null);
    try {
        if (definition.getTimeout() > TransactionDefinition.TIMEOUT_DEFAULT) {
            this.uowManager.setUOWTimeout(uowType, definition.getTimeout());
        }
        if (debug) {
            logger.debug("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
        }
        UOWActionAdapter<T> action = new UOWActionAdapter<>(definition, callback, (uowType == UOWManager.UOW_TYPE_GLOBAL_TRANSACTION), !joinTx, newSynch, debug);
        this.uowManager.runUnderUOW(uowType, joinTx, action);
        if (debug) {
            logger.debug("Returned from WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
        }
        return action.getResult();
    } catch (UOWException ex) {
        throw new TransactionSystemException("UOWManager transaction processing failed", ex);
    } catch (UOWActionException ex) {
        throw new TransactionSystemException("UOWManager threw unexpected UOWActionException", ex);
    } finally {
        if (suspendedResources != null) {
            resume(null, suspendedResources);
        }
    }
}
Also used : UOWActionException(com.ibm.wsspi.uow.UOWActionException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) InvalidTimeoutException(org.springframework.transaction.InvalidTimeoutException) UOWException(com.ibm.wsspi.uow.UOWException) NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException)

Example 13 with TransactionSystemException

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

the class AbstractTransactionAspectTests method doTestRollbackOnException.

/**
	 * Check that the given exception thrown by the target can produce the
	 * desired behavior with the appropriate transaction attribute.
	 * @param ex exception to be thrown by the target
	 * @param shouldRollback whether this should cause a transaction rollback
	 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute() {

        @Override
        public boolean rollbackOn(Throwable t) {
            assertTrue(t == ex);
            return shouldRollback;
        }
    };
    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Gets additional call(s) from TransactionControl
    given(ptm.getTransaction(txatt)).willReturn(status);
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
        if (shouldRollback) {
            willThrow(tex).given(ptm).rollback(status);
        } else {
            willThrow(tex).given(ptm).commit(status);
        }
    }
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.exceptional(ex);
        fail("Should have thrown exception");
    } catch (Throwable t) {
        if (rollbackException) {
            assertEquals("Caught wrong exception", tex, t);
        } else {
            assertEquals("Caught wrong exception", ex, t);
        }
    }
    if (!rollbackException) {
        if (shouldRollback) {
            verify(ptm).rollback(status);
        } else {
            verify(ptm).commit(status);
        }
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) Method(java.lang.reflect.Method) TransactionSystemException(org.springframework.transaction.TransactionSystemException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 14 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project ignite by apache.

the class SpringTransactionManager method doRollback.

/** {@inheritDoc} */
@Override
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
    IgniteTransactionObject txObj = (IgniteTransactionObject) status.getTransaction();
    Transaction tx = txObj.getTransactionHolder().getTransaction();
    if (status.isDebug() && log.isDebugEnabled())
        log.debug("Rolling back Ignite transaction: " + tx);
    try {
        tx.rollback();
    } catch (IgniteException e) {
        throw new TransactionSystemException("Could not rollback Ignite transaction", e);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) TransactionSystemException(org.springframework.transaction.TransactionSystemException)

Example 15 with TransactionSystemException

use of org.springframework.transaction.TransactionSystemException in project uPortal by Jasig.

the class JpaEventAggregationManagementDaoTest method testAcademicTermDetails.

@Test
public void testAcademicTermDetails() throws Exception {
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final List<AcademicTermDetail> academicTermDetail = eventAggregationManagementDao.getAcademicTermDetails();
            assertEquals(0, academicTermDetail.size());
            List<AcademicTermDetail> academicTermDetails = new ArrayList<AcademicTermDetail>();
            academicTermDetails.add(new AcademicTermDetailImpl(new DateMidnight(2012, 1, 1), new DateMidnight(2012, 6, 1), "Spring 2012"));
            academicTermDetails.add(new AcademicTermDetailImpl(new DateMidnight(2012, 6, 1), new DateMidnight(2012, 9, 1), "Summer 2012"));
            academicTermDetails.add(new AcademicTermDetailImpl(new DateMidnight(2012, 9, 1), new DateMidnight(2013, 1, 1), "Fall 2012"));
            eventAggregationManagementDao.setAcademicTermDetails(academicTermDetails);
        }
    });
    try {
        this.executeInTransaction(new CallableWithoutResult() {

            @Override
            protected void callWithoutResult() {
                final List<AcademicTermDetail> academicTermDetail = eventAggregationManagementDao.getAcademicTermDetails();
                assertEquals(3, academicTermDetail.size());
                academicTermDetail.add(new AcademicTermDetailImpl(new DateMidnight(2012, 1, 1), new DateMidnight(2013, 6, 1), "Spring 2013"));
                try {
                    eventAggregationManagementDao.setAcademicTermDetails(academicTermDetail);
                    fail();
                } catch (IllegalArgumentException e) {
                //expected
                }
            }
        });
        fail();
    } catch (TransactionSystemException e) {
    //Expected
    }
    this.executeInTransaction(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final List<AcademicTermDetail> academicTermDetail = eventAggregationManagementDao.getAcademicTermDetails();
            assertEquals(3, academicTermDetail.size());
            academicTermDetail.get(0).setTermName("New Term");
            academicTermDetail.remove(2);
            eventAggregationManagementDao.setAcademicTermDetails(academicTermDetail);
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final List<AcademicTermDetail> academicTermDetail = eventAggregationManagementDao.getAcademicTermDetails();
            assertEquals(2, academicTermDetail.size());
        }
    });
}
Also used : DateMidnight(org.joda.time.DateMidnight) AcademicTermDetail(org.apereo.portal.events.aggr.AcademicTermDetail) ArrayList(java.util.ArrayList) List(java.util.List) TransactionSystemException(org.springframework.transaction.TransactionSystemException) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BaseAggrEventsJpaDaoTest(org.apereo.portal.test.BaseAggrEventsJpaDaoTest)

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