From 558cbdd97db0575ac2468c9db4529ebe8470a21c Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Sun, 27 May 2018 10:10:00 -0400 Subject: [PATCH] HHH-12640 Update to JBossStandAloneJtaPlatform should be backward compatible attempting old names as well --- .../internal/StandardJtaPlatformResolver.java | 19 ++++++ .../WildFlyStandAloneJtaPlatform.java | 63 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WildFlyStandAloneJtaPlatform.java diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/StandardJtaPlatformResolver.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/StandardJtaPlatformResolver.java index 5e0e8e3e78..2cdd4e21cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/StandardJtaPlatformResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/StandardJtaPlatformResolver.java @@ -45,6 +45,25 @@ public class StandardJtaPlatformResolver implements JtaPlatformResolver { // IMPL NOTE : essentially we attempt Class lookups and use the exceptions from the class(es) not // being found as the indicator + // first try loading WildFly Transaction Client + try { + classLoaderService.classForName( WildFlyStandAloneJtaPlatform.WILDFLY_TM_CLASS_NAME ); + classLoaderService.classForName( WildFlyStandAloneJtaPlatform.WILDFLY_UT_CLASS_NAME ); + + // we know that the WildFly Transaction Client TM classes are available + // if neither of these look-ups resulted in an error (no such class), then WildFly Transaction Client TM available on + // the classpath. + // + // todo : we cannot really distinguish between the need for JBossStandAloneJtaPlatform versus JBossApServerJtaPlatform + // but discussions with David led to the JtaPlatformProvider solution above, so inside JBoss AS we + // should be relying on that. + // Note that on WF13+, we can expect org.jboss.as.jpa.hibernate5.service.WildFlyCustomJtaPlatformInitiator to choose + // the WildFlyCustomJtaPlatform, unless the application has disabled WildFlyCustomJtaPlatformInitiator. + return new WildFlyStandAloneJtaPlatform(); + } + catch (ClassLoadingException ignore) { + } + // JBoss ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ try { diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WildFlyStandAloneJtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WildFlyStandAloneJtaPlatform.java new file mode 100644 index 0000000000..8725a427ba --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WildFlyStandAloneJtaPlatform.java @@ -0,0 +1,63 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ + +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.engine.transaction.jta.platform.internal; + +import javax.transaction.TransactionManager; +import javax.transaction.UserTransaction; + +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException; + +/** + * Return a standalone JTA transaction manager for WildFly transaction client + * Known to work for WildFly 13+ + * + * @author Scott Marlow + */ +public class WildFlyStandAloneJtaPlatform extends AbstractJtaPlatform { + public static final String WILDFLY_TM_CLASS_NAME = "org.wildfly.transaction.client.ContextTransactionManager"; + public static final String WILDFLY_UT_CLASS_NAME = "org.wildfly.transaction.client.LocalUserTransaction"; + + @Override + protected TransactionManager locateTransactionManager() { + try { + final Class wildflyTmClass = serviceRegistry() + .getService( ClassLoaderService.class ) + .classForName( WILDFLY_TM_CLASS_NAME ); + return (TransactionManager) wildflyTmClass.getMethod( "getInstance" ).invoke( null ); + } + catch (Exception e) { + throw new JtaPlatformException( + "Could not obtain WildFly Transaction Client transaction manager instance", + e + ); + } + } + + @Override + protected UserTransaction locateUserTransaction() { + try { + final Class jbossUtClass = serviceRegistry() + .getService( ClassLoaderService.class ) + .classForName( WILDFLY_UT_CLASS_NAME ); + return (UserTransaction) jbossUtClass.getMethod( "getInstance" ).invoke( null ); + } + catch (Exception e) { + throw new JtaPlatformException( + "Could not obtain WildFly Transaction Client user transaction instance", + e + ); + } + } +}