Search in sources :

Example 66 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class CloseEntityManagerWithActiveTransactionTest method tearDown.

@After
public void tearDown() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        em.createQuery("delete from Muffin").executeUpdate();
        em.createQuery("delete from Box").executeUpdate();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    } catch (Exception e) {
        final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager();
        if (transactionManager.getTransaction() != null && transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE) {
            TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
        }
        throw e;
    } finally {
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) After(org.junit.After)

Example 67 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class CloseEntityManagerWithActiveTransactionTest method testPersistThenCloseWithAnActiveTransaction.

@Test
@TestForIssue(jiraKey = "HHH-10942")
public void testPersistThenCloseWithAnActiveTransaction() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        Box box = new Box();
        box.setColor("red-and-white");
        em.persist(box);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    } catch (Exception e) {
        final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager();
        if (transactionManager.getTransaction() != null && transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE) {
            TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
        }
        throw e;
    } finally {
        if (em.isOpen()) {
            em.close();
        }
    }
    em = getOrCreateEntityManager();
    try {
        final List results = em.createQuery("from Box").getResultList();
        assertThat(results.size(), is(1));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) List(java.util.List) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 68 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class CloseEntityManagerWithActiveTransactionTest method testMergeThenCloseWithAnActiveTransaction.

@Test
@TestForIssue(jiraKey = "HHH-11166")
public void testMergeThenCloseWithAnActiveTransaction() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        Box box = new Box();
        box.setColor("red-and-white");
        em.persist(box);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
        em = getOrCreateEntityManager();
        Muffin muffin = new Muffin();
        muffin.setKind("blueberry");
        box.addMuffin(muffin);
        em.merge(box);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    } catch (Exception e) {
        final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager();
        if (transactionManager.getTransaction() != null && transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE) {
            TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
        }
        throw e;
    } finally {
        if (em.isOpen()) {
            em.close();
        }
    }
    em = getOrCreateEntityManager();
    try {
        final List<Box> boxes = em.createQuery("from Box").getResultList();
        assertThat(boxes.size(), is(1));
        assertThat(boxes.get(0).getMuffinSet().size(), is(1));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 69 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class CloseEntityManagerWithActiveTransactionTest method testMergeWithDeletionOrphanRemovalThenCloseWithAnActiveTransaction.

@Test
@TestForIssue(jiraKey = "HHH-11269")
public void testMergeWithDeletionOrphanRemovalThenCloseWithAnActiveTransaction() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        Muffin muffin = new Muffin();
        muffin.setKind("blueberry");
        SmallBox box = new SmallBox(muffin);
        box.setColor("red-and-white");
        em.persist(box);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
        em = getOrCreateEntityManager();
        box.emptyBox();
        em.merge(box);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    } catch (Exception e) {
        final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager();
        if (transactionManager.getTransaction() != null && transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE) {
            TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
        }
        throw e;
    } finally {
        if (em.isOpen()) {
            em.close();
        }
    }
    em = getOrCreateEntityManager();
    try {
        final List<SmallBox> boxes = em.createQuery("from SmallBox").getResultList();
        assertThat(boxes.size(), is(1));
        assertTrue(boxes.get(0).isEmpty());
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 70 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class CloseEntityManagerWithActiveTransactionTest method testRemoveThenCloseWithAnActiveTransaction.

@Test
@TestForIssue(jiraKey = "HHH-11166")
public void testRemoveThenCloseWithAnActiveTransaction() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        Box box = new Box();
        box.setColor("red-and-white");
        em.persist(box);
        Muffin muffin = new Muffin();
        muffin.setKind("blueberry");
        box.addMuffin(muffin);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
        em = getOrCreateEntityManager();
        box = em.find(Box.class, box.getId());
        em.remove(box);
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    } catch (Exception e) {
        final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager();
        if (transactionManager.getTransaction() != null && transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE) {
            TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
        }
        throw e;
    } finally {
        if (em.isOpen()) {
            em.close();
        }
    }
    em = getOrCreateEntityManager();
    try {
        final List<Box> boxes = em.createQuery("from Box").getResultList();
        assertThat(boxes.size(), is(0));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5