Search in sources :

Example 71 with TransactionManager

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

the class CloseEntityManagerWithActiveTransactionTest method testUpdateThenCloseWithAnActiveTransaction.

@Test
@TestForIssue(jiraKey = "HHH-11166")
public void testUpdateThenCloseWithAnActiveTransaction() 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();
        box = em.find(Box.class, box.getId());
        Muffin muffin = new Muffin();
        muffin.setKind("blueberry");
        box.addMuffin(muffin);
        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 72 with TransactionManager

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

the class CloseEntityManagerWithActiveTransactionTest method testCommitReleasesLogicalConnection.

@Test
@TestForIssue(jiraKey = "HHH-11099")
public void testCommitReleasesLogicalConnection() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        Box box = new Box();
        box.setColor("red-and-white");
        em.persist(box);
        final SessionImpl session = (SessionImpl) em.unwrap(Session.class);
        final JdbcCoordinatorImpl jdbcCoordinator = (JdbcCoordinatorImpl) session.getJdbcCoordinator();
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
        assertThat("The logical connection is still open after commit", jdbcCoordinator.getLogicalConnection().isOpen(), is(false));
    } 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 : JdbcCoordinatorImpl(org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl) EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) SessionImpl(org.hibernate.internal.SessionImpl) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 73 with TransactionManager

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

the class TxUtil method markRollbackOnly.

public static void markRollbackOnly(boolean useJta, Session s) {
    if (useJta) {
        JtaPlatform jtaPlatform = s.getSessionFactory().getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class);
        TransactionManager tm = jtaPlatform.retrieveTransactionManager();
        try {
            tm.setRollbackOnly();
        } catch (SystemException e) {
            throw new RuntimeException(e);
        }
    } else {
        s.getTransaction().markRollbackOnly();
    }
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager)

Example 74 with TransactionManager

use of javax.transaction.TransactionManager in project graphdb by neo4j-attic.

the class MasterImpl method rollbackThisAndResumeOther.

void rollbackThisAndResumeOther(Transaction otherTx, SlaveContext txId) {
    try {
        TransactionManager txManager = graphDbConfig.getTxModule().getTxManager();
        txManager.rollback();
        transactions.remove(txId);
        if (otherTx != null) {
            txManager.resume(otherTx);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) IllegalResourceException(org.neo4j.kernel.impl.transaction.IllegalResourceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException)

Example 75 with TransactionManager

use of javax.transaction.TransactionManager in project graphdb by neo4j-attic.

the class TestKernelPanic method panicTest.

@Test
public void panicTest() throws Exception {
    String path = "target/var/testdb";
    AbstractNeo4jTestCase.deleteFileOrDirectory(new File(path));
    EmbeddedGraphDatabase graphDb = new EmbeddedGraphDatabase(path);
    XaDataSourceManager xaDs = graphDb.getConfig().getTxModule().getXaDataSourceManager();
    IllBehavingXaDataSource noob = new IllBehavingXaDataSource();
    xaDs.registerDataSource("noob", noob, UTF8.encode("554342"));
    Panic panic = new Panic();
    graphDb.registerKernelEventHandler(panic);
    org.neo4j.graphdb.Transaction gdbTx = graphDb.beginTx();
    TransactionManager txMgr = graphDb.getConfig().getTxModule().getTxManager();
    Transaction tx = txMgr.getTransaction();
    graphDb.createNode();
    tx.enlistResource(noob.getXaConnection().getXaResource());
    try {
        gdbTx.success();
        gdbTx.finish();
        fail("Should fail");
    } catch (Throwable t) {
        // ok
        for (int i = 0; i < 10 && panic.panic == false; i++) {
            Thread.sleep(1000);
        }
    } finally {
        graphDb.unregisterKernelEventHandler(panic);
    }
    assertTrue(panic.panic);
    graphDb.shutdown();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) XaDataSourceManager(org.neo4j.kernel.impl.transaction.XaDataSourceManager) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) File(java.io.File) Test(org.junit.Test)

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