Search in sources :

Example 11 with CannotCreateTransactionException

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

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(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)11 TransactionException (org.springframework.transaction.TransactionException)4 InvalidIsolationLevelException (org.springframework.transaction.InvalidIsolationLevelException)3 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)3 Method (java.lang.reflect.Method)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 ConnectionHolder (org.springframework.jdbc.datasource.ConnectionHolder)2 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)2 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)2 TransactionContext (com.hazelcast.transaction.TransactionContext)1 Connection (jakarta.jms.Connection)1 ConnectionFactory (jakarta.jms.ConnectionFactory)1 Session (jakarta.jms.Session)1 EntityManager (javax.persistence.EntityManager)1 NotSupportedException (javax.resource.NotSupportedException)1 Connection (javax.resource.cci.Connection)1