Search in sources :

Example 1 with RollbackException

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();
}
Also used : ArrayList(java.util.ArrayList) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(jakarta.persistence.RollbackException) Test(org.junit.jupiter.api.Test)

Example 2 with RollbackException

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();
}
Also used : ArrayList(java.util.ArrayList) RollbackException(jakarta.persistence.RollbackException) Test(org.junit.jupiter.api.Test)

Aggregations

RollbackException (jakarta.persistence.RollbackException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)1