use of cn.taketoday.transaction.jta.JtaTransactionManager 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.jta.JtaTransactionManager in project today-infrastructure by TAKETODAY.
the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndSynchronizationOnActual.
@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationOnActual() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
tt.setTimeout(10);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(ut).setTransactionTimeout(10);
verify(ut).begin();
verify(ut).rollback();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
use of cn.taketoday.transaction.jta.JtaTransactionManager in project today-infrastructure by TAKETODAY.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThatExceptionOfType(TransactionSuspensionNotSupportedException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
}));
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
}
use of cn.taketoday.transaction.jta.JtaTransactionManager in project today-infrastructure by TAKETODAY.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndExceptionAndNoGlobalRollback.
@Test
public void jtaTransactionManagerWithExistingTransactionAndExceptionAndNoGlobalRollback() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
ptm.setGlobalRollbackOnParticipationFailure(false);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThatIllegalStateException().isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.registerSynchronization(synch);
throw new IllegalStateException("I want a rollback");
}
}));
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
use of cn.taketoday.transaction.jta.JtaTransactionManager in project today-infrastructure by TAKETODAY.
the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndSynchronizationNever.
@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationNever() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronizationName("SYNCHRONIZATION_NEVER");
tt.setTimeout(10);
ptm.afterPropertiesSet();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
status.setRollbackOnly();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(ut).setTransactionTimeout(10);
verify(ut).begin();
verify(ut, atLeastOnce()).getStatus();
verify(ut).rollback();
}
Aggregations