Search in sources :

Example 1 with PlatformTransactionManager

use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.

the class TransactionTemplate method execute.

@Override
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
    PlatformTransactionManager transactionManager = getTransactionManager();
    Assert.state(transactionManager != null, "No PlatformTransactionManager set");
    if (transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
        return ((CallbackPreferringPlatformTransactionManager) transactionManager).execute(this, action);
    } else {
        TransactionStatus status = transactionManager.getTransaction(this);
        T result;
        try {
            result = action.doInTransaction(status);
        } catch (RuntimeException | Error ex) {
            // Transactional code threw application exception -> rollback
            rollbackOnException(transactionManager, status, ex);
            throw ex;
        } catch (Throwable ex) {
            // Transactional code threw unexpected exception -> rollback
            rollbackOnException(transactionManager, status, ex);
            throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
        }
        transactionManager.commit(status);
        return result;
    }
}
Also used : UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Nullable(cn.taketoday.lang.Nullable)

Example 2 with PlatformTransactionManager

use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.

the class AbstractTransactionAspectTests method transactionShouldSucceed.

/**
 * Check that a transaction is created and committed.
 */
@Test
public void transactionShouldSucceed() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(getNameMethod, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // expect a transaction
    given(ptm.getTransaction(txatt)).willReturn(status);
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);
    verify(ptm).commit(status);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 3 with PlatformTransactionManager

use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.

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) {
            assertThat(t == ex).isTrue();
            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) {
            assertThat(t).as("Caught wrong exception").isEqualTo(tex);
        } else {
            assertThat(t).as("Caught wrong exception").isEqualTo(ex);
        }
    }
    if (!rollbackException) {
        if (shouldRollback) {
            verify(ptm).rollback(status);
        } else {
            verify(ptm).commit(status);
        }
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Method(java.lang.reflect.Method) TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager)

Example 4 with PlatformTransactionManager

use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.

the class AbstractTransactionAspectTests method twoTransactionsShouldSucceed.

/**
 * Check that two transactions are created and committed.
 */
@Test
public void twoTransactionsShouldSucceed() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource();
    tas1.register(getNameMethod, txatt);
    MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource();
    tas2.register(setNameMethod, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // expect a transaction
    given(ptm.getTransaction(txatt)).willReturn(status);
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, new TransactionAttributeSource[] { tas1, tas2 });
    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);
    itb.setName("myName");
    checkTransactionStatus(false);
    verify(ptm, times(2)).commit(status);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 5 with PlatformTransactionManager

use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.

the class AbstractTransactionAspectTests method transactionShouldSucceedWithNotNew.

/**
 * Check that a transaction is created and committed.
 */
@Test
public void transactionShouldSucceedWithNotNew() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(getNameMethod, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // expect a transaction
    given(ptm.getTransaction(txatt)).willReturn(status);
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    checkTransactionStatus(false);
    // verification!?
    itb.getName();
    checkTransactionStatus(false);
    verify(ptm).commit(status);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)58 Test (org.junit.jupiter.api.Test)42 TransactionStatus (cn.taketoday.transaction.TransactionStatus)26 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)20 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)20 BeanFactory (cn.taketoday.beans.factory.BeanFactory)16 Method (java.lang.reflect.Method)14 TransactionManager (cn.taketoday.transaction.TransactionManager)10 Nullable (cn.taketoday.lang.Nullable)6 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)6 Properties (java.util.Properties)6 DataSource (javax.sql.DataSource)6 TransactionSystemException (cn.taketoday.transaction.TransactionSystemException)4 TransactionInfo (cn.taketoday.transaction.interceptor.TransactionAspectSupport.TransactionInfo)4 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)4 Connection (java.sql.Connection)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 TransactionAttribute (cn.taketoday.transaction.interceptor.TransactionAttribute)3 BeansException (cn.taketoday.beans.BeansException)2 BeanFactoryAware (cn.taketoday.beans.factory.BeanFactoryAware)2