Search in sources :

Example 1 with SynchronizationCollectorImpl

use of org.hibernate.test.resource.common.SynchronizationCollectorImpl in project hibernate-orm by hibernate.

the class BasicJdbcTransactionTests method basicUsageTest.

@Test
public void basicUsageTest() throws Exception {
    final TransactionCoordinatorOwnerTestingImpl owner = new TransactionCoordinatorOwnerTestingImpl();
    final JdbcResourceLocalTransactionCoordinatorBuilderImpl transactionCoordinatorBuilder = new JdbcResourceLocalTransactionCoordinatorBuilderImpl();
    final TransactionCoordinator transactionCoordinator = transactionCoordinatorBuilder.buildTransactionCoordinator(owner, new TransactionCoordinatorBuilder.Options() {

        @Override
        public boolean shouldAutoJoinTransaction() {
            return false;
        }
    });
    SynchronizationCollectorImpl sync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(sync);
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(0, sync.getBeforeCompletionCount());
    assertEquals(0, sync.getSuccessfulCompletionCount());
    assertEquals(0, sync.getFailedCompletionCount());
    transactionCoordinator.getTransactionDriverControl().commit();
    assertEquals(1, sync.getBeforeCompletionCount());
    assertEquals(1, sync.getSuccessfulCompletionCount());
    assertEquals(0, sync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) Test(org.junit.Test)

Example 2 with SynchronizationCollectorImpl

use of org.hibernate.test.resource.common.SynchronizationCollectorImpl in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method rollbackBmtUsageTest.

@Test
public void rollbackBmtUsageTest() throws Exception {
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    // begin the transaction
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    // create and add a local Synchronization
    SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(localSync);
    // rollback the transaction
    transactionCoordinator.getTransactionDriverControl().rollback();
    // post conditions
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertEquals(0, localSync.getBeforeCompletionCount());
    assertEquals(0, localSync.getSuccessfulCompletionCount());
    assertEquals(1, localSync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 3 with SynchronizationCollectorImpl

use of org.hibernate.test.resource.common.SynchronizationCollectorImpl in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method assureMultipleJoinCallsNoOp.

@Test
public void assureMultipleJoinCallsNoOp() throws Exception {
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction
    tm.begin();
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(false);
    // no auto-join now
    assertFalse(transactionCoordinator.isJoined());
    transactionCoordinator.explicitJoin();
    assertTrue(transactionCoordinator.isJoined());
    transactionCoordinator.explicitJoin();
    transactionCoordinator.explicitJoin();
    transactionCoordinator.explicitJoin();
    transactionCoordinator.explicitJoin();
    assertTrue(transactionCoordinator.isJoined());
    // create and add a local Synchronization
    SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(localSync);
    // commit the transaction
    tm.commit();
    // post conditions
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertEquals(1, localSync.getBeforeCompletionCount());
    assertEquals(1, localSync.getSuccessfulCompletionCount());
    assertEquals(0, localSync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 4 with SynchronizationCollectorImpl

use of org.hibernate.test.resource.common.SynchronizationCollectorImpl 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)

Example 5 with SynchronizationCollectorImpl

use of org.hibernate.test.resource.common.SynchronizationCollectorImpl in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method basicBmtUsageTest.

@Test
public void basicBmtUsageTest() throws Exception {
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    // begin the transaction
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    // create and add a local Synchronization
    SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(localSync);
    // commit the transaction
    transactionCoordinator.getTransactionDriverControl().commit();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertEquals(1, localSync.getBeforeCompletionCount());
    assertEquals(1, localSync.getSuccessfulCompletionCount());
    assertEquals(0, localSync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Aggregations

SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)10 Test (org.junit.Test)10 TransactionManager (javax.transaction.TransactionManager)8 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)8 LocalSynchronizationException (org.hibernate.resource.transaction.LocalSynchronizationException)1 NullSynchronizationException (org.hibernate.resource.transaction.NullSynchronizationException)1 JdbcResourceLocalTransactionCoordinatorBuilderImpl (org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl)1 SynchronizationRegistryStandardImpl (org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl)1 TransactionCoordinator (org.hibernate.resource.transaction.spi.TransactionCoordinator)1 TransactionCoordinatorBuilder (org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder)1