Search in sources :

Example 1 with SynchronizationRegistryStandardImpl

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

the class SynchronizationRegistryStandardImplTests method testUserSynchronizationExceptions.

@Test
public void testUserSynchronizationExceptions() {
    // exception in beforeCompletion
    SynchronizationRegistryStandardImpl registry = new SynchronizationRegistryStandardImpl();
    Synchronization synchronization = SynchronizationErrorImpl.forBefore();
    registry.registerSynchronization(synchronization);
    try {
        registry.notifySynchronizationsBeforeTransactionCompletion();
        fail("Expecting LocalSynchronizationException, but call succeeded");
    } catch (LocalSynchronizationException expected) {
    // expected
    } catch (Exception e) {
        fail("Was expecting LocalSynchronizationException, but got " + e.getClass().getName());
    }
    // exception in beforeCompletion
    registry.clearSynchronizations();
    registry = new SynchronizationRegistryStandardImpl();
    synchronization = SynchronizationErrorImpl.forAfter();
    registry.registerSynchronization(synchronization);
    try {
        registry.notifySynchronizationsAfterTransactionCompletion(Status.STATUS_COMMITTED);
        fail("Expecting LocalSynchronizationException, but call succeeded");
    } catch (LocalSynchronizationException expected) {
    // expected
    } catch (Exception e) {
        fail("Was expecting LocalSynchronizationException, but got " + e.getClass().getName());
    }
}
Also used : LocalSynchronizationException(org.hibernate.resource.transaction.LocalSynchronizationException) SynchronizationRegistryStandardImpl(org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl) Synchronization(javax.transaction.Synchronization) LocalSynchronizationException(org.hibernate.resource.transaction.LocalSynchronizationException) NullSynchronizationException(org.hibernate.resource.transaction.NullSynchronizationException) Test(org.junit.Test)

Example 2 with SynchronizationRegistryStandardImpl

use of org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl 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)2 NullSynchronizationException (org.hibernate.resource.transaction.NullSynchronizationException)2 SynchronizationRegistryStandardImpl (org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl)2 Test (org.junit.Test)2 Synchronization (javax.transaction.Synchronization)1 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)1