Search in sources :

Example 11 with JtaTransactionCoordinatorImpl

use of org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method testMarkRollbackOnly.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testMarkRollbackOnly() throws Exception {
    JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, JtaPlatformStandardTestingImpl.INSTANCE, preferUserTransactions(), true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(TransactionStatus.ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
    assertEquals(TransactionStatus.MARKED_ROLLBACK, transactionCoordinator.getTransactionDriverControl().getStatus());
    try {
        transactionCoordinator.getTransactionDriverControl().commit();
    } catch (TransactionException expected) {
    } finally {
        assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    }
}
Also used : TransactionException(org.hibernate.TransactionException) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 12 with JtaTransactionCoordinatorImpl

use of org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method testSynchronizationFailure.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testSynchronizationFailure() throws Exception {
    JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, JtaPlatformStandardTestingImpl.INSTANCE, preferUserTransactions(), true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(SynchronizationErrorImpl.forBefore());
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(TransactionStatus.ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    try {
        transactionCoordinator.getTransactionDriverControl().commit();
    } catch (Exception expected) {
    } finally {
        assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) TransactionException(org.hibernate.TransactionException) TransactionRequiredForJoinException(org.hibernate.resource.transaction.TransactionRequiredForJoinException) SystemException(javax.transaction.SystemException) Test(org.junit.Test)

Example 13 with JtaTransactionCoordinatorImpl

use of org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl in project hibernate-orm by hibernate.

the class InaccessibleJtaPlatformTests method testInaccessibleUserTransactionHandling.

@Test
public void testInaccessibleUserTransactionHandling() {
    // first, have JtaPlatform throw an exception
    try {
        final JtaPlatformInaccessibleImpl jtaPlatform = new JtaPlatformInaccessibleImpl(true);
        final TransactionCoordinator transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, jtaPlatform, false, false);
        transactionCoordinator.getTransactionDriverControl().begin();
        fail("Expecting JtaPlatformInaccessibleException, but call succeeded");
    } catch (JtaPlatformInaccessibleException expected) {
    // expected condition
    } catch (Exception e) {
        fail("Expecting JtaPlatformInaccessibleException, but got " + e.getClass().getName());
    }
    // then, have it return null
    try {
        final JtaPlatformInaccessibleImpl jtaPlatform = new JtaPlatformInaccessibleImpl(false);
        final TransactionCoordinator transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, jtaPlatform, false, false);
        transactionCoordinator.getTransactionDriverControl().begin();
        fail("Expecting JtaPlatformInaccessibleException, but call succeeded");
    } catch (JtaPlatformInaccessibleException expected) {
    // expected condition
    } catch (Exception e) {
        fail("Expecting JtaPlatformInaccessibleException, but got " + e.getClass().getName());
    }
}
Also used : JtaPlatformInaccessibleException(org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) JtaPlatformInaccessibleException(org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException) Test(org.junit.Test)

Example 14 with JtaTransactionCoordinatorImpl

use of org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl 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)

Example 15 with JtaTransactionCoordinatorImpl

use of org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method basicCmtUsageTest.

@Test
public void basicCmtUsageTest() throws Exception {
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction
    tm.begin();
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(true);
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    // NOTE : because of auto-join
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    // 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)

Aggregations

JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)23 Test (org.junit.Test)22 TransactionManager (javax.transaction.TransactionManager)12 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)8 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)7 EntityManager (javax.persistence.EntityManager)6 Session (org.hibernate.Session)2 TransactionException (org.hibernate.TransactionException)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 TransactionRequiredForJoinException (org.hibernate.resource.transaction.TransactionRequiredForJoinException)2 JtaPlatformInaccessibleException (org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException)2 TransactionCoordinator (org.hibernate.resource.transaction.spi.TransactionCoordinator)2 AbstractJPATest (org.hibernate.test.jpa.AbstractJPATest)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SystemException (javax.transaction.SystemException)1