From 22ffaea0c322769a14e93781cae3f06c98b7093f Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 4 Nov 2015 20:55:52 -0600 Subject: [PATCH] HHH-10248 - map removed TransactionFactory classes --- .../BootstrapServiceRegistryBuilder.java | 14 ++++ .../internal/StrategySelectorBuilder.java | 17 +++++ .../internal/log/DeprecationLogger.java | 7 ++ ...ransactionCoordinatorBuilderInitiator.java | 28 ++++++- .../LegacySettingInitiatorTest.java | 76 +++++++++++++++++++ 5 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/resource/transaction/LegacySettingInitiatorTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java index 0efb15e5ed..a890004682 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java @@ -20,6 +20,7 @@ import org.hibernate.boot.registry.selector.StrategyRegistrationProvider; import org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder; import org.hibernate.integrator.internal.IntegratorServiceImpl; import org.hibernate.integrator.spi.Integrator; +import org.hibernate.service.ServiceRegistry; /** * Builder for {@link BootstrapServiceRegistry} instances. Provides registry for services needed for @@ -222,4 +223,17 @@ public class BootstrapServiceRegistryBuilder { integratorService ); } + + /** + * Destroy a service registry. Applications should only destroy registries they have explicitly created. + * + * @param serviceRegistry The registry to be closed. + */ + public static void destroy(ServiceRegistry serviceRegistry) { + if ( serviceRegistry == null ) { + return; + } + + ( (BootstrapServiceRegistryImpl) serviceRegistry ).destroy(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorBuilder.java index 1252f5036a..c4005836dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorBuilder.java @@ -354,6 +354,23 @@ public class StrategySelectorBuilder { JtaTransactionCoordinatorBuilderImpl.SHORT_NAME, JtaTransactionCoordinatorBuilderImpl.class ); + + // add the legacy TransactionFactory impl names... + strategySelector.registerStrategyImplementor( + TransactionCoordinatorBuilder.class, + "org.hibernate.transaction.JDBCTransactionFactory", + JdbcResourceLocalTransactionCoordinatorBuilderImpl.class + ); + strategySelector.registerStrategyImplementor( + TransactionCoordinatorBuilder.class, + "org.hibernate.transaction.JTATransactionFactory", + JtaTransactionCoordinatorBuilderImpl.class + ); + strategySelector.registerStrategyImplementor( + TransactionCoordinatorBuilder.class, + "org.hibernate.transaction.CMTTransactionFactory", + JtaTransactionCoordinatorBuilderImpl.class + ); } private void addMultiTableBulkIdStrategies(StrategySelectorImpl strategySelector) { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/log/DeprecationLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/log/DeprecationLogger.java index 9f3c7509c5..15c9ec543c 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/log/DeprecationLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/log/DeprecationLogger.java @@ -193,4 +193,11 @@ public interface DeprecationLogger extends BasicLogger { value = "Found use of deprecated entity-type selector syntax in HQL/JPQL query ['%1$s.class']; use TYPE operator instead : type(%1$s)" ) void logDeprecationOfClassEntityTypeSelector(String path); + + @LogMessage(level = WARN) + @Message( + id = 90000018, + value = "Found use of deprecated transaction factory setting [%s]; use the new TransactionCoordinatorBuilder settings [%s] instead" + ) + void logDeprecatedTransactionFactorySetting(String legacySettingName, String updatedSettingName); } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/transaction/internal/TransactionCoordinatorBuilderInitiator.java b/hibernate-core/src/main/java/org/hibernate/resource/transaction/internal/TransactionCoordinatorBuilderInitiator.java index 335c72a31e..d39dca3927 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/transaction/internal/TransactionCoordinatorBuilderInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/transaction/internal/TransactionCoordinatorBuilderInitiator.java @@ -11,10 +11,13 @@ import java.util.Map; import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.resource.transaction.TransactionCoordinatorBuilder; import org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl; import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.jboss.logging.Logger; + /** * StandardServiceInitiator for initiating the TransactionCoordinatorBuilder service. * @@ -22,6 +25,8 @@ import org.hibernate.service.spi.ServiceRegistryImplementor; * @author Steve Ebersole */ public class TransactionCoordinatorBuilderInitiator implements StandardServiceInitiator { + public static final String LEGACY_SETTING_NAME = "hibernate.transaction.factory_class"; + /** * Singleton access */ @@ -29,15 +34,32 @@ public class TransactionCoordinatorBuilderInitiator implements StandardServiceIn @Override public TransactionCoordinatorBuilder initiateService(Map configurationValues, ServiceRegistryImplementor registry) { - final Object strategy = configurationValues.get( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY ); - return registry.getService( StrategySelector.class ).resolveDefaultableStrategy( TransactionCoordinatorBuilder.class, - strategy, + determineStrategySelection( configurationValues ), JdbcResourceLocalTransactionCoordinatorBuilderImpl.INSTANCE ); } + private static Object determineStrategySelection(Map configurationValues) { + final Object coordinatorStrategy = configurationValues.get( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY ); + if ( coordinatorStrategy != null ) { + return coordinatorStrategy; + } + + final Object legacySetting = configurationValues.get( LEGACY_SETTING_NAME ); + if ( legacySetting != null ) { + DeprecationLogger.DEPRECATION_LOGGER.logDeprecatedTransactionFactorySetting( + LEGACY_SETTING_NAME, + AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY + ); + return legacySetting; + } + + // triggers the default + return null; + } + @Override public Class getServiceInitiated() { return TransactionCoordinatorBuilder.class; diff --git a/hibernate-core/src/test/java/org/hibernate/test/resource/transaction/LegacySettingInitiatorTest.java b/hibernate-core/src/test/java/org/hibernate/test/resource/transaction/LegacySettingInitiatorTest.java new file mode 100644 index 0000000000..eb1a2fa382 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/resource/transaction/LegacySettingInitiatorTest.java @@ -0,0 +1,76 @@ +/* + * 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.test.resource.transaction; + +import java.util.Collections; + +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; +import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl; +import org.hibernate.resource.transaction.TransactionCoordinatorBuilder; +import org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl; +import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl; +import org.hibernate.resource.transaction.internal.TransactionCoordinatorBuilderInitiator; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertThat; + +/** + * @author Steve Ebersole + */ +public class LegacySettingInitiatorTest extends BaseUnitTestCase { + private BootstrapServiceRegistryImpl bsr; + + @Before + public void before() { + bsr = (BootstrapServiceRegistryImpl) new BootstrapServiceRegistryBuilder().build(); + } + + @After + public void after() { + if ( bsr != null ) { + bsr.destroy(); + } + } + + @Test + public void testLegacySettingSelection() { + final TransactionCoordinatorBuilderInitiator initiator = new TransactionCoordinatorBuilderInitiator(); + + TransactionCoordinatorBuilder builder = initiator.initiateService( + Collections.singletonMap( + TransactionCoordinatorBuilderInitiator.LEGACY_SETTING_NAME, + "org.hibernate.transaction.JDBCTransactionFactory" + ), + bsr + ); + assertThat( builder, instanceOf( JdbcResourceLocalTransactionCoordinatorBuilderImpl.class ) ); + + builder = initiator.initiateService( + Collections.singletonMap( + TransactionCoordinatorBuilderInitiator.LEGACY_SETTING_NAME, + "org.hibernate.transaction.JTATransactionFactory" + ), + bsr + ); + assertThat( builder, instanceOf( JtaTransactionCoordinatorBuilderImpl.class ) ); + + builder = initiator.initiateService( + Collections.singletonMap( + TransactionCoordinatorBuilderInitiator.LEGACY_SETTING_NAME, + "org.hibernate.transaction.CMTTransactionFactory" + ), + bsr + ); + assertThat( builder, instanceOf( JtaTransactionCoordinatorBuilderImpl.class ) ); + } +}