use of jakarta.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerSerializationTests method serializable.
@Test
public void serializable() throws Exception {
UserTransaction ut1 = mock(UserTransaction.class);
UserTransaction ut2 = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
JtaTransactionManager jtam = new JtaTransactionManager();
jtam.setUserTransaction(ut1);
jtam.setTransactionManager(tm);
jtam.setRollbackOnCommitFailure(true);
jtam.afterPropertiesSet();
SimpleNamingContextBuilder jndiEnv = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
jndiEnv.bind(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut2);
JtaTransactionManager serializedJtatm = SerializationTestUtils.serializeAndDeserialize(jtam);
// should do client-side lookup
assertThat(serializedJtatm.logger).as("Logger must survive serialization").isNotNull();
assertThat(serializedJtatm.getUserTransaction() == ut2).as("UserTransaction looked up on client").isTrue();
assertThat(serializedJtatm.getTransactionManager()).as("TransactionManager didn't survive").isNull();
assertThat(serializedJtatm.isRollbackOnCommitFailure()).isTrue();
}
use of jakarta.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithCommit.
@Test
public void jtaTransactionManagerWithCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setName("txName");
assertThat(ptm.getTransactionSynchronization()).isEqualTo(JtaTransactionManager.SYNCHRONIZATION_ALWAYS);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isNull();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.registerSynchronization(synch);
assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isNull();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
verify(ut).begin();
verify(ut).commit();
verify(synch).beforeCommit(false);
verify(synch).beforeCompletion();
verify(synch).afterCommit();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
}
use of jakarta.transaction.UserTransaction in project spring-framework by spring-projects.
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 jakarta.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationSupports.
@Test
public void jtaTransactionManagerWithPropagationSupports() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
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(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
use of jakarta.transaction.UserTransaction in project spring-framework by spring-projects.
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);
}
Aggregations