Search in sources :

Example 1 with NullSynchronizationException

use of org.hibernate.resource.transaction.NullSynchronizationException in project hibernate-orm by hibernate.

the class SynchronizationRegistryStandardImplTests method basicUsageTests.

@Test
public void basicUsageTests() {
    final SynchronizationRegistryStandardImpl registry = new SynchronizationRegistryStandardImpl();
    try {
        registry.registerSynchronization(null);
        fail("Was expecting NullSynchronizationException, but call succeeded");
    } catch (NullSynchronizationException expected) {
    // expected behavior
    } catch (Exception e) {
        fail("Was expecting NullSynchronizationException, but got " + e.getClass().getName());
    }
    final SynchronizationCollectorImpl synchronization = new SynchronizationCollectorImpl();
    assertEquals(0, registry.getNumberOfRegisteredSynchronizations());
    registry.registerSynchronization(synchronization);
    assertEquals(1, registry.getNumberOfRegisteredSynchronizations());
    registry.registerSynchronization(synchronization);
    assertEquals(1, registry.getNumberOfRegisteredSynchronizations());
    assertEquals(0, synchronization.getBeforeCompletionCount());
    assertEquals(0, synchronization.getSuccessfulCompletionCount());
    assertEquals(0, synchronization.getFailedCompletionCount());
    {
        registry.notifySynchronizationsBeforeTransactionCompletion();
        assertEquals(1, synchronization.getBeforeCompletionCount());
        assertEquals(0, synchronization.getSuccessfulCompletionCount());
        assertEquals(0, synchronization.getFailedCompletionCount());
        registry.notifySynchronizationsAfterTransactionCompletion(Status.STATUS_COMMITTED);
        assertEquals(1, synchronization.getBeforeCompletionCount());
        assertEquals(1, synchronization.getSuccessfulCompletionCount());
        assertEquals(0, synchronization.getFailedCompletionCount());
    }
    // afterQuery completion should clear registered synchronizations
    assertEquals(0, registry.getNumberOfRegisteredSynchronizations());
    // reset the sync
    synchronization.reset();
    assertEquals(0, synchronization.getBeforeCompletionCount());
    assertEquals(0, synchronization.getSuccessfulCompletionCount());
    assertEquals(0, synchronization.getFailedCompletionCount());
    // re-register it
    registry.registerSynchronization(synchronization);
    assertEquals(1, registry.getNumberOfRegisteredSynchronizations());
    {
        registry.notifySynchronizationsAfterTransactionCompletion(Status.STATUS_ROLLEDBACK);
        assertEquals(0, synchronization.getBeforeCompletionCount());
        assertEquals(0, synchronization.getSuccessfulCompletionCount());
        assertEquals(1, synchronization.getFailedCompletionCount());
        // afterQuery completion should clear registered synchronizations
        assertEquals(0, registry.getNumberOfRegisteredSynchronizations());
    }
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) NullSynchronizationException(org.hibernate.resource.transaction.NullSynchronizationException) SynchronizationRegistryStandardImpl(org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl) LocalSynchronizationException(org.hibernate.resource.transaction.LocalSynchronizationException) NullSynchronizationException(org.hibernate.resource.transaction.NullSynchronizationException) Test(org.junit.Test)

Aggregations

LocalSynchronizationException (org.hibernate.resource.transaction.LocalSynchronizationException)1 NullSynchronizationException (org.hibernate.resource.transaction.NullSynchronizationException)1 SynchronizationRegistryStandardImpl (org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl)1 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)1 Test (org.junit.Test)1