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));
}
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));
}
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();
}
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();
}
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();
}
Aggregations