use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.
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();
}
use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.
the class JdbcTransactionManagerTests method testParticipatingTransactionWithRollbackOnlyAndInnerSynch.
@Test
public void testParticipatingTransactionWithRollbackOnlyAndInnerSynch() throws Exception {
tm.setTransactionSynchronization(JdbcTransactionManager.SYNCHRONIZATION_NEVER);
JdbcTransactionManager tm2 = new JdbcTransactionManager(ds);
// tm has no synch enabled (used at outer level), tm2 has synch enabled (inner level)
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());
final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_UNKNOWN);
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
final TransactionTemplate tt = new TransactionTemplate(tm2);
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();
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
tm.commit(ts);
});
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();
}
use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.
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);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
// first: rollback
ptm.rollback(status);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
// second: commit attempt
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> ptm.commit(status));
verify(ut).begin();
verify(ut).rollback();
}
use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.
the class JtaTransactionManagerTests method doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly.
private void doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly(boolean failEarly) throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK);
JtaTransactionManager tm = newJtaTransactionManager(ut);
if (failEarly) {
tm.setFailEarlyOnGlobalRollbackOnly(true);
}
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
boolean outerTransactionBoundaryReached = false;
try {
assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
TransactionTemplate tt = new TransactionTemplate(tm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCompletion(int status) {
assertThat(status == TransactionSynchronization.STATUS_ROLLED_BACK).as("Correct completion status").isTrue();
}
});
}
});
outerTransactionBoundaryReached = true;
tm.commit(ts);
fail("Should have thrown UnexpectedRollbackException");
} catch (UnexpectedRollbackException ex) {
// expected
if (!outerTransactionBoundaryReached) {
tm.rollback(ts);
}
assertThat(outerTransactionBoundaryReached).isNotEqualTo(failEarly);
}
verify(ut).begin();
if (failEarly) {
verify(ut).rollback();
} else {
verify(ut).commit();
}
}
use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.
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);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
// first rollback
ptm.rollback(status);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
// second rollback attempt
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> ptm.rollback(status));
verify(ut).begin();
verify(ut).rollback();
}
Aggregations