Search in sources :

Example 16 with UnexpectedRollbackException

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

the class DataSourceTransactionManagerTests method doTestParticipatingTransactionWithRollbackOnly.

private void doTestParticipatingTransactionWithRollbackOnly(boolean failEarly) throws Exception {
    given(con.isReadOnly()).willReturn(false);
    if (failEarly) {
        tm.setFailEarlyOnGlobalRollbackOnly(true);
    }
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_ROLLED_BACK);
    TransactionSynchronizationManager.registerSynchronization(synch);
    boolean outerTransactionBoundaryReached = false;
    try {
        assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
        final TransactionTemplate tt = new TransactionTemplate(tm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                boolean condition1 = !status.isNewTransaction();
                assertThat(condition1).as("Is existing transaction").isTrue();
                assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
                tt.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
                        assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
                        boolean condition = !status.isNewTransaction();
                        assertThat(condition).as("Is existing transaction").isTrue();
                        status.setRollbackOnly();
                    }
                });
                boolean condition = !status.isNewTransaction();
                assertThat(condition).as("Is existing transaction").isTrue();
                assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
            }
        });
        outerTransactionBoundaryReached = true;
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
        // expected
        if (!outerTransactionBoundaryReached) {
            tm.rollback(ts);
        }
        if (failEarly) {
            assertThat(outerTransactionBoundaryReached).isFalse();
        } else {
            assertThat(outerTransactionBoundaryReached).isTrue();
        }
    }
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    assertThat(synch.beforeCommitCalled).isFalse();
    assertThat(synch.beforeCompletionCalled).isTrue();
    assertThat(synch.afterCommitCalled).isFalse();
    assertThat(synch.afterCompletionCalled).isTrue();
    verify(con).rollback();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 17 with UnexpectedRollbackException

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

the class AbstractTransactionAspectTests method cannotCommitTransaction.

/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = setNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    // Method m2 = getNameMethod;
    // No attributes for m2
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    TransactionStatus status = mock(TransactionStatus.class);
    given(ptm.getTransaction(txatt)).willReturn(status);
    UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
    willThrow(ex).given(ptm).commit(status);
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    String name = "new name";
    try {
        itb.setName(name);
        fail("Shouldn't have succeeded");
    } catch (UnexpectedRollbackException thrown) {
        assertThat(thrown == ex).isTrue();
    }
    // Should have invoked target and changed name
    assertThat(itb.getName() == name).isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 18 with UnexpectedRollbackException

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

the class AbstractReactiveTransactionAspectTests method cannotCommitTransaction.

/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = setNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    // Method m2 = getNameMethod;
    // No attributes for m2
    ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
    ReactiveTransaction status = mock(ReactiveTransaction.class);
    given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
    UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
    given(rtm.commit(status)).willReturn(Mono.error(ex));
    given(rtm.rollback(status)).willReturn(Mono.empty());
    DefaultTestBean tb = new DefaultTestBean();
    TestBean itb = (TestBean) advised(tb, rtm, tas);
    String name = "new name";
    Mono.from(itb.setName(name)).as(StepVerifier::create).consumeErrorWith(throwable -> {
        assertThat(throwable.getClass()).isEqualTo(RuntimeException.class);
        assertThat(throwable.getCause()).isEqualTo(ex);
    }).verify();
    // Should have invoked target and changed name
    itb.getName().as(StepVerifier::create).expectNext(name).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) Fail.fail(org.assertj.core.api.Fail.fail) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Test(org.junit.jupiter.api.Test) TransactionSystemException(org.springframework.transaction.TransactionSystemException) BDDMockito.given(org.mockito.BDDMockito.given) TransactionContext(org.springframework.transaction.reactive.TransactionContext) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Method(java.lang.reflect.Method) Mockito.mock(org.mockito.Mockito.mock) Method(java.lang.reflect.Method) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 19 with UnexpectedRollbackException

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

the class JdbcTransactionManagerTests method doTestParticipatingTransactionWithRollbackOnly.

private void doTestParticipatingTransactionWithRollbackOnly(boolean failEarly) throws Exception {
    given(con.isReadOnly()).willReturn(false);
    if (failEarly) {
        tm.setFailEarlyOnGlobalRollbackOnly(true);
    }
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_ROLLED_BACK);
    TransactionSynchronizationManager.registerSynchronization(synch);
    boolean outerTransactionBoundaryReached = false;
    try {
        assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
        final TransactionTemplate tt = new TransactionTemplate(tm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                boolean condition1 = !status.isNewTransaction();
                assertThat(condition1).as("Is existing transaction").isTrue();
                assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
                tt.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
                        assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
                        boolean condition = !status.isNewTransaction();
                        assertThat(condition).as("Is existing transaction").isTrue();
                        status.setRollbackOnly();
                    }
                });
                boolean condition = !status.isNewTransaction();
                assertThat(condition).as("Is existing transaction").isTrue();
                assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
            }
        });
        outerTransactionBoundaryReached = true;
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
        // expected
        if (!outerTransactionBoundaryReached) {
            tm.rollback(ts);
        }
        if (failEarly) {
            assertThat(outerTransactionBoundaryReached).isFalse();
        } else {
            assertThat(outerTransactionBoundaryReached).isTrue();
        }
    }
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    assertThat(synch.beforeCommitCalled).isFalse();
    assertThat(synch.beforeCompletionCalled).isTrue();
    assertThat(synch.afterCommitCalled).isFalse();
    assertThat(synch.afterCompletionCalled).isTrue();
    verify(con).rollback();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 20 with UnexpectedRollbackException

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

the class JmsTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
    Session session = txObject.getResourceHolder().getOriginalSession();
    if (session != null) {
        try {
            if (status.isDebug()) {
                logger.debug("Committing JMS transaction on Session [" + session + "]");
            }
            session.commit();
        } catch (TransactionRolledBackException ex) {
            throw new UnexpectedRollbackException("JMS transaction rolled back", ex);
        } catch (JMSException ex) {
            throw new TransactionSystemException("Could not commit JMS transaction", ex);
        }
    }
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionRolledBackException(jakarta.jms.TransactionRolledBackException) JMSException(jakarta.jms.JMSException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) Session(jakarta.jms.Session)

Aggregations

UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)20 Valid (javax.validation.Valid)11 VBoardException (com.vsct.vboard.models.VBoardException)6 User (com.vsct.vboard.models.User)5 DateTime (org.joda.time.DateTime)5 TransactionStatus (org.springframework.transaction.TransactionStatus)4 Comment (com.vsct.vboard.models.Comment)3 Pin (com.vsct.vboard.models.Pin)3 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)3 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)3 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)3 TransactionSystemException (org.springframework.transaction.TransactionSystemException)3 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)3 Method (java.lang.reflect.Method)2 MimeMessage (javax.mail.internet.MimeMessage)2 Test (org.junit.jupiter.api.Test)2 TransactionException (org.springframework.transaction.TransactionException)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 JMSException (jakarta.jms.JMSException)1