Search in sources :

Example 26 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project jOOQ by jOOQ.

the class SpringTransactionProvider method begin.

@Override
public void begin(TransactionContext ctx) {
    log.info("Begin transaction");
    // This TransactionProvider behaves like jOOQ's DefaultTransactionProvider,
    // which supports nested transactions using Savepoints
    TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition(PROPAGATION_NESTED));
    ctx.transaction(new SpringTransaction(tx));
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 27 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-boot by spring-projects.

the class SpringTransactionProvider method begin.

@Override
public void begin(TransactionContext context) {
    TransactionDefinition definition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_NESTED);
    TransactionStatus status = this.transactionManager.getTransaction(definition);
    context.transaction(new SpringTransaction(status));
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 28 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition 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);
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_ROLLED_BACK);
    TransactionSynchronizationManager.registerSynchronization(synch);
    boolean outerTransactionBoundaryReached = false;
    try {
        assertTrue("Is new transaction", ts.isNewTransaction());
        final TransactionTemplate tt = new TransactionTemplate(tm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Is existing transaction", !status.isNewTransaction());
                assertFalse("Is not rollback-only", status.isRollbackOnly());
                tt.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
                        assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                        assertTrue("Is existing transaction", !status.isNewTransaction());
                        status.setRollbackOnly();
                    }
                });
                assertTrue("Is existing transaction", !status.isNewTransaction());
                assertTrue("Is rollback-only", status.isRollbackOnly());
            }
        });
        outerTransactionBoundaryReached = true;
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
        // expected
        if (!outerTransactionBoundaryReached) {
            tm.rollback(ts);
        }
        if (failEarly) {
            assertFalse(outerTransactionBoundaryReached);
        } else {
            assertTrue(outerTransactionBoundaryReached);
        }
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertFalse(synch.beforeCommitCalled);
    assertTrue(synch.beforeCompletionCalled);
    assertFalse(synch.afterCommitCalled);
    assertTrue(synch.afterCompletionCalled);
    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 29 with DefaultTransactionDefinition

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndCommit.

@Test
public void jtaTransactionManagerWithRollbackAndCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
    // first: rollback
    ptm.rollback(status);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        // second: commit attempt
        ptm.commit(status);
        fail("Should have thrown IllegalTransactionStateException");
    } catch (IllegalTransactionStateException ex) {
    // expected
    }
    verify(ut).begin();
    verify(ut).rollback();
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.Test)

Example 30 with DefaultTransactionDefinition

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithDoubleRollback.

@Test
public void jtaTransactionManagerWithDoubleRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
    // first rollback
    ptm.rollback(status);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        // second rollback attempt
        ptm.rollback(status);
        fail("Should have thrown IllegalTransactionStateException");
    } catch (IllegalTransactionStateException ex) {
    // expected
    }
    verify(ut).begin();
    verify(ut).rollback();
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.Test)

Aggregations

DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)45 TransactionStatus (org.springframework.transaction.TransactionStatus)32 Test (org.junit.Test)28 TransactionCallback (org.springframework.transaction.support.TransactionCallback)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 Connection (javax.jms.Connection)7 ConnectionFactory (javax.jms.ConnectionFactory)7 Session (javax.jms.Session)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)7 UserTransaction (javax.transaction.UserTransaction)5 SessionCallback (org.springframework.jms.core.SessionCallback)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)4 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 UOWException (com.ibm.wsspi.uow.UOWException)2 DataAccessException (org.springframework.dao.DataAccessException)2 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)2 NestedTransactionNotSupportedException (org.springframework.transaction.NestedTransactionNotSupportedException)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2