Search in sources :

Example 1 with CannotCreateTransactionException

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

the class AbstractTransactionAspectTests method cannotCreateTransaction.

/**
 * Simulate a transaction infrastructure failure.
 * Shouldn't invoke target method.
 */
@Test
public void cannotCreateTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Expect a transaction
    CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
    given(ptm.getTransaction(txatt)).willThrow(ex);
    TestBean tb = new TestBean() {

        @Override
        public String getName() {
            throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
        }
    };
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.getName();
        fail("Shouldn't have invoked method");
    } catch (CannotCreateTransactionException thrown) {
        assertThat(thrown == ex).isTrue();
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) Method(java.lang.reflect.Method) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 2 with CannotCreateTransactionException

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

the class DataSourceTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
    Connection con = null;
    try {
        if (!txObject.hasConnectionHolder() || txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
            Connection newCon = obtainDataSource().getConnection();
            if (logger.isDebugEnabled()) {
                logger.debug("Acquired Connection [{}] for JDBC transaction", newCon);
            }
            txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
        }
        ConnectionHolder connectionHolder = txObject.getConnectionHolder();
        connectionHolder.setSynchronizedWithTransaction(true);
        con = connectionHolder.getConnection();
        Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
        txObject.setPreviousIsolationLevel(previousIsolationLevel);
        txObject.setReadOnly(definition.isReadOnly());
        // configured the connection pool to set it already).
        if (con.getAutoCommit()) {
            txObject.setMustRestoreAutoCommit(true);
            if (logger.isDebugEnabled()) {
                logger.debug("Switching JDBC Connection [{}] to manual commit", con);
            }
            con.setAutoCommit(false);
        }
        prepareTransactionalConnection(con, definition);
        connectionHolder.setTransactionActive(true);
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            connectionHolder.setTimeoutInSeconds(timeout);
        }
        // Bind the connection holder to the thread.
        if (txObject.isNewConnectionHolder()) {
            TransactionSynchronizationManager.bindResource(obtainDataSource(), connectionHolder);
        }
    } catch (Throwable ex) {
        if (txObject.isNewConnectionHolder()) {
            DataSourceUtils.releaseConnection(con, obtainDataSource());
            txObject.setConnectionHolder(null, false);
        }
        throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
    }
}
Also used : CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) Connection(java.sql.Connection)

Example 3 with CannotCreateTransactionException

use of cn.taketoday.transaction.CannotCreateTransactionException in project today-framework by TAKETODAY.

the class AbstractTransactionAspectTests method cannotCreateTransaction.

/**
 * Simulate a transaction infrastructure failure.
 * Shouldn't invoke target method.
 */
@Test
public void cannotCreateTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Expect a transaction
    CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
    given(ptm.getTransaction(txatt)).willThrow(ex);
    TestBean tb = new TestBean() {

        @Override
        public String getName() {
            throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
        }
    };
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.getName();
        fail("Shouldn't have invoked method");
    } catch (CannotCreateTransactionException thrown) {
        assertThat(thrown == ex).isTrue();
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) Method(java.lang.reflect.Method) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 4 with CannotCreateTransactionException

use of cn.taketoday.transaction.CannotCreateTransactionException in project today-framework by TAKETODAY.

the class DataSourceTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
    Connection con = null;
    try {
        if (!txObject.hasConnectionHolder() || txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
            Connection newCon = obtainDataSource().getConnection();
            if (logger.isDebugEnabled()) {
                logger.debug("Acquired Connection [{}] for JDBC transaction", newCon);
            }
            txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
        }
        ConnectionHolder connectionHolder = txObject.getConnectionHolder();
        connectionHolder.setSynchronizedWithTransaction(true);
        con = connectionHolder.getConnection();
        Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
        txObject.setPreviousIsolationLevel(previousIsolationLevel);
        txObject.setReadOnly(definition.isReadOnly());
        // configured the connection pool to set it already).
        if (con.getAutoCommit()) {
            txObject.setMustRestoreAutoCommit(true);
            if (logger.isDebugEnabled()) {
                logger.debug("Switching JDBC Connection [{}] to manual commit", con);
            }
            con.setAutoCommit(false);
        }
        prepareTransactionalConnection(con, definition);
        connectionHolder.setTransactionActive(true);
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            connectionHolder.setTimeoutInSeconds(timeout);
        }
        // Bind the connection holder to the thread.
        if (txObject.isNewConnectionHolder()) {
            TransactionSynchronizationManager.bindResource(obtainDataSource(), connectionHolder);
        }
    } catch (Throwable ex) {
        if (txObject.isNewConnectionHolder()) {
            DataSourceUtils.releaseConnection(con, obtainDataSource());
            txObject.setConnectionHolder(null, false);
        }
        throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
    }
}
Also used : CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) Connection(java.sql.Connection)

Example 5 with CannotCreateTransactionException

use of cn.taketoday.transaction.CannotCreateTransactionException in project today-framework by TAKETODAY.

the class AbstractReactiveTransactionAspectTests method cannotCreateTransaction.

/**
 * Simulate a transaction infrastructure failure.
 * Shouldn't invoke target method.
 */
@Test
public void cannotCreateTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
    // Expect a transaction
    CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
    given(rtm.getReactiveTransaction(txatt)).willThrow(ex);
    DefaultTestBean tb = new DefaultTestBean() {

        @Override
        public Mono<String> getName() {
            throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
        }
    };
    TestBean itb = (TestBean) advised(tb, rtm, tas);
    itb.getName().as(StepVerifier::create).expectError(CannotCreateTransactionException.class).verify();
}
Also used : CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) ReactiveTransactionManager(cn.taketoday.transaction.ReactiveTransactionManager) Method(java.lang.reflect.Method) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

CannotCreateTransactionException (cn.taketoday.transaction.CannotCreateTransactionException)6 Method (java.lang.reflect.Method)4 Test (org.junit.jupiter.api.Test)4 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)2 ReactiveTransactionManager (cn.taketoday.transaction.ReactiveTransactionManager)2 Connection (java.sql.Connection)2 StepVerifier (reactor.test.StepVerifier)2