Search in sources :

Example 21 with JtaTransactionCoordinatorImpl

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

the class InaccessibleJtaPlatformTests method testInaccessibleTransactionManagerHandling.

@Test
public void testInaccessibleTransactionManagerHandling() {
    // 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 22 with JtaTransactionCoordinatorImpl

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

the class SynchronizationTypeTest method testImplicitJoining.

@Test
public void testImplicitJoining() throws Exception {
    // here the transaction is started beforeQuery the EM is opened.  Because the SynchronizationType is UNSYNCHRONIZED
    // though, it should not auto join the transaction
    assertFalse("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    EntityManager entityManager = entityManagerFactory().createEntityManager(SynchronizationType.UNSYNCHRONIZED, null);
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertFalse("EM was auto joined on creation", transactionCoordinator.isSynchronizationRegistered());
    assertTrue("EM was auto joined on creation", transactionCoordinator.isActive());
    assertFalse("EM was auto joined on creation", transactionCoordinator.isJoined());
    session.getFlushMode();
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertFalse(transactionCoordinator.isJoined());
    entityManager.joinTransaction();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 23 with JtaTransactionCoordinatorImpl

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

the class TransactionJoiningTest method testImplicitJoining.

@Test
public void testImplicitJoining() throws Exception {
    // here the transaction is started beforeQuery the EM is opened...
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager entityManager = entityManagerFactory().createEntityManager();
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) 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