Search in sources :

Example 1 with TransactionManager

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

the class AbstractNonFunctionalTest method withTx.

protected <T> T withTx(NodeEnvironment environment, SharedSessionContractImplementor session, Callable<T> callable) throws Exception {
    if (jtaPlatform != null) {
        TransactionManager tm = environment.getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager();
        return Caches.withinTx(tm, callable);
    } else {
        Transaction transaction = ((Session) session).beginTransaction();
        boolean rollingBack = false;
        try {
            T retval = callable.call();
            if (transaction.getStatus() == TransactionStatus.ACTIVE) {
                transaction.commit();
            } else {
                rollingBack = true;
                transaction.rollback();
            }
            return retval;
        } catch (Exception e) {
            if (!rollingBack) {
                try {
                    transaction.rollback();
                } catch (Exception suppressed) {
                    e.addSuppressed(suppressed);
                }
            }
            throw e;
        }
    }
}
Also used : BatchModeJtaPlatform(org.hibernate.test.cache.infinispan.util.BatchModeJtaPlatform) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(org.hibernate.Transaction) TransactionManager(javax.transaction.TransactionManager) Session(org.hibernate.Session)

Example 2 with TransactionManager

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

the class DdlInWildFlyUsingBmtAndEmfTest method testCreateThenDrop.

@Test
public void testCreateThenDrop() throws Exception {
    URL persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource(PERSISTENCE_XML_RESOURCE_NAME);
    if (persistenceXmlUrl == null) {
        persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource('/' + PERSISTENCE_XML_RESOURCE_NAME);
    }
    assertNotNull(persistenceXmlUrl);
    ParsedPersistenceXmlDescriptor persistenceUnit = PersistenceXmlParser.locateIndividualPersistenceUnit(persistenceXmlUrl);
    // creating the EMF causes SchemaCreator to be run...
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, Collections.emptyMap()).build();
    // closing the EMF causes the delayed SchemaDropper to be run...
    //		wrap in a transaction just to see if we can get this to fail in the way the WF report says;
    //		in my experience however this succeeds with or without the transaction
    final TransactionManager tm = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager();
    tm.begin();
    Transaction txn = tm.getTransaction();
    emf.close();
    txn.commit();
}
Also used : ParsedPersistenceXmlDescriptor(org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor) JBossAppServerJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) TransactionManager(javax.transaction.TransactionManager) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) URL(java.net.URL) Test(org.junit.Test)

Example 3 with TransactionManager

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

the class BeforeCompletionFailureTest method testUniqueConstraintViolationDuringManagedFlush.

@Test
@TestForIssue(jiraKey = "HHH-9888")
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // set up test data
    Session session = openSession();
    session.getTransaction().begin();
    session.save(newEntity(1));
    session.getTransaction().commit();
    session.close();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // do the test
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction ("CMT" style)
    tm.begin();
    session = openSession();
    session.save(newEntity(2));
    // which should lead to the UK violation
    try {
        tm.commit();
        fail("Expecting a failure from JTA commit");
    } catch (RollbackException expected) {
        log.info("Test encountered expected JTA RollbackException; looking for nested JDBCException", expected);
        boolean violationExceptionFound = false;
        Throwable cause = expected;
        while (cause != null) {
            if (cause instanceof JDBCException) {
                log.info("Found JDBCException, assuming related to UK violation", cause);
                violationExceptionFound = true;
                break;
            }
            cause = cause.getCause();
        }
        if (!violationExceptionFound) {
            fail("Did not find JDBCException in JTA RollbackException chain");
        }
    } finally {
        if (!((SessionImplementor) session).isClosed()) {
            session.close();
        }
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // clean up test data
    session = openSession();
    session.getTransaction().begin();
    session.createQuery("delete SimpleEntity").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : JDBCException(org.hibernate.JDBCException) TransactionManager(javax.transaction.TransactionManager) RollbackException(javax.transaction.RollbackException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with TransactionManager

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

the class TxUtil method withTxSession.

public static void withTxSession(JtaPlatform jtaPlatform, SessionBuilder sessionBuilder, ThrowingConsumer<Session, Exception> consumer) throws Exception {
    if (jtaPlatform != null) {
        TransactionManager tm = jtaPlatform.retrieveTransactionManager();
        final SessionBuilder sb = sessionBuilder;
        Caches.withinTx(tm, () -> {
            withSession(sb, s -> {
                consumer.accept(s);
                s.flush();
            });
            return null;
        });
    } else {
        withSession(sessionBuilder, s -> withResourceLocalTx(s, consumer));
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) SessionBuilder(org.hibernate.SessionBuilder)

Example 5 with TransactionManager

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

the class TxUtil method withTxSessionApply.

public static <T> T withTxSessionApply(JtaPlatform jtaPlatform, SessionBuilder sessionBuilder, ThrowingFunction<Session, T, Exception> function) throws Exception {
    if (jtaPlatform != null) {
        TransactionManager tm = jtaPlatform.retrieveTransactionManager();
        Callable<T> callable = () -> withSessionApply(sessionBuilder, s -> {
            T t = function.apply(s);
            s.flush();
            return t;
        });
        return Caches.withinTx(tm, callable);
    } else {
        return withSessionApply(sessionBuilder, s -> withResourceLocalTx(s, function));
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager)

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