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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations