Search in sources :

Example 1 with JtaPlatform

use of org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform in project hibernate-orm by hibernate.

the class TxUtil method withTxSession.

public static void withTxSession(boolean useJta, SessionFactory sessionFactory, ThrowingConsumer<Session, Exception> consumer) throws Exception {
    JtaPlatform jtaPlatform = useJta ? sessionFactory.getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class) : null;
    withTxSession(jtaPlatform, sessionFactory.withOptions(), consumer);
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)

Example 2 with JtaPlatform

use of org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform 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 3 with JtaPlatform

use of org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform in project hibernate-orm by hibernate.

the class JTASessionContext method currentSession.

@Override
public Session currentSession() throws HibernateException {
    final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService(JtaPlatform.class);
    final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    if (transactionManager == null) {
        throw new HibernateException("No TransactionManagerLookup specified");
    }
    Transaction txn;
    try {
        txn = transactionManager.getTransaction();
        if (txn == null) {
            throw new HibernateException("Unable to locate current JTA transaction");
        }
        if (!JtaStatusHelper.isActive(txn.getStatus())) {
            // entries cleaned up (aside from spawning threads).
            throw new HibernateException("Current transaction is not in progress");
        }
    } catch (HibernateException e) {
        throw e;
    } catch (Throwable t) {
        throw new HibernateException("Problem locating/validating JTA transaction", t);
    }
    final Object txnIdentifier = jtaPlatform.getTransactionIdentifier(txn);
    Session currentSession = currentSessionMap.get(txnIdentifier);
    if (currentSession == null) {
        currentSession = buildOrObtainSession();
        try {
            txn.registerSynchronization(buildCleanupSynch(txnIdentifier));
        } catch (Throwable t) {
            try {
                currentSession.close();
            } catch (Throwable ignore) {
                LOG.debug("Unable to release generated current-session on failed synch registration", ignore);
            }
            throw new HibernateException("Unable to register cleanup Synchronization with TransactionManager");
        }
        currentSessionMap.put(txnIdentifier, currentSession);
    } else {
        validateExistingSession(currentSession);
    }
    return currentSession;
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) HibernateException(org.hibernate.HibernateException) TransactionManager(javax.transaction.TransactionManager) Session(org.hibernate.Session)

Example 4 with JtaPlatform

use of org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform in project hibernate-orm by hibernate.

the class StandardJtaPlatformResolver method resolveJtaPlatform.

@Override
public JtaPlatform resolveJtaPlatform(Map configurationValues, ServiceRegistryImplementor registry) {
    final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);
    // Initially look for a JtaPlatformProvider
    for (JtaPlatformProvider provider : classLoaderService.loadJavaServices(JtaPlatformProvider.class)) {
        final JtaPlatform providedPlatform = provider.getProvidedJtaPlatform();
        log.tracef("Located JtaPlatformProvider [%s] provided JtaPlaform : %s", provider, providedPlatform);
        if (providedPlatform != null) {
            return providedPlatform;
        }
    }
    // JBoss ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    try {
        classLoaderService.classForName(JBossStandAloneJtaPlatform.JBOSS_TM_CLASS_NAME);
        classLoaderService.classForName(JBossStandAloneJtaPlatform.JBOSS_UT_CLASS_NAME);
        // should be relying on that
        return new JBossStandAloneJtaPlatform();
    } catch (ClassLoadingException ignore) {
    }
    // Bitronix ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    try {
        classLoaderService.classForName(BitronixJtaPlatform.TM_CLASS_NAME);
        return new BitronixJtaPlatform();
    } catch (ClassLoadingException ignore) {
    }
    // JOnAS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    try {
        classLoaderService.classForName(JOnASJtaPlatform.TM_CLASS_NAME);
        return new JOnASJtaPlatform();
    } catch (ClassLoadingException ignore) {
    }
    // JOTM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    try {
        classLoaderService.classForName(JOTMJtaPlatform.TM_CLASS_NAME);
        return new JOTMJtaPlatform();
    } catch (ClassLoadingException ignore) {
    }
    // WebSphere Liberty ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    try {
        classLoaderService.classForName(WebSphereLibertyJtaPlatform.TMF_CLASS_NAME);
        return new WebSphereLibertyJtaPlatform();
    } catch (ClassLoadingException ignore) {
    }
    // WebSphere traditional ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    for (WebSphereJtaPlatform.WebSphereEnvironment webSphereEnvironment : WebSphereJtaPlatform.WebSphereEnvironment.values()) {
        try {
            Class accessClass = classLoaderService.classForName(webSphereEnvironment.getTmAccessClassName());
            return new WebSphereJtaPlatform(accessClass, webSphereEnvironment);
        } catch (ClassLoadingException ignore) {
        }
    }
    // Finally, return the default...
    log.debugf("Could not resolve JtaPlatform, using default [%s]", NoJtaPlatform.class.getName());
    return NoJtaPlatform.INSTANCE;
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) JtaPlatformProvider(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformProvider) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 5 with JtaPlatform

use of org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform 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)

Aggregations

JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)8 TransactionManager (javax.transaction.TransactionManager)3 Session (org.hibernate.Session)2 SystemException (javax.transaction.SystemException)1 Transaction (javax.transaction.Transaction)1 HibernateException (org.hibernate.HibernateException)1 Transaction (org.hibernate.Transaction)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)1 JtaPlatformProvider (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformProvider)1 JtaPlatformResolver (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver)1 BatchModeJtaPlatform (org.hibernate.test.cache.infinispan.util.BatchModeJtaPlatform)1