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