Search in sources :

Example 6 with JtaTransactionCoordinatorImpl

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

the class JtaTransactionJoiningTest method testExplicitJoining.

@Test
public void testExplicitJoining() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    SessionImplementor session = (SessionImplementor) sessionFactory().withOptions().autoJoinTransactions(false).openSession();
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertFalse(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    // causes a call to TransactionCoordinator#pulse
    session.getFlushMode();
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertFalse(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    session.getFlushMode();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertFalse(transactionCoordinator.isJoined());
    transactionCoordinator.explicitJoin();
    session.getFlushMode();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isJoined());
    ((Session) session).close();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Session(org.hibernate.Session) Test(org.junit.Test) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest)

Example 7 with JtaTransactionCoordinatorImpl

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

the class JtaTransactionJoiningTest method control.

@Test
public void control() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    SessionImplementor session = (SessionImplementor) sessionFactory().openSession();
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    ((Session) session).close();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Session(org.hibernate.Session) Test(org.junit.Test) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest)

Example 8 with JtaTransactionCoordinatorImpl

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

the class AbstractBasicJtaTestScenarios method explicitJoinOutsideTxnTest.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void explicitJoinOutsideTxnTest() throws Exception {
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(false);
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // try to force a join, should fail...
    try {
        transactionCoordinator.explicitJoin();
        fail("Expecting explicitJoin() call outside of transaction to fail");
    } catch (TransactionRequiredForJoinException expected) {
    }
}
Also used : TransactionRequiredForJoinException(org.hibernate.resource.transaction.TransactionRequiredForJoinException) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 9 with JtaTransactionCoordinatorImpl

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

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

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