use of jakarta.persistence.RollbackException in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionCommitWithRollbackException.
@Test
public void testTransactionCommitWithRollbackException() {
given(manager.getTransaction()).willReturn(tx);
given(tx.getRollbackOnly()).willReturn(true);
willThrow(new RollbackException()).given(tx).commit();
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
try {
Object result = tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
});
assertThat(result).isSameAs(l);
} catch (TransactionSystemException tse) {
// expected
boolean condition = tse.getCause() instanceof RollbackException;
assertThat(condition).isTrue();
}
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
verify(manager).flush();
verify(manager).close();
}
use of jakarta.persistence.RollbackException in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testParticipatingTransactionWithRollbackOnly.
@Test
public void testParticipatingTransactionWithRollbackOnly() {
given(manager.getTransaction()).willReturn(tx);
given(tx.isActive()).willReturn(true);
given(tx.getRollbackOnly()).willReturn(true);
willThrow(new RollbackException()).given(tx).commit();
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
return tt.execute(status1 -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status1.setRollbackOnly();
return null;
});
})).withCauseInstanceOf(RollbackException.class);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
verify(manager).flush();
verify(tx).setRollbackOnly();
verify(manager).close();
}
Aggregations