From eacbd7895685f473d83968ef004dcc1507be938b Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 4 Sep 2024 08:33:08 +0200 Subject: [PATCH] split out SessionFactorySettings and kill off MappingMetamodelImpl.close() Signed-off-by: Gavin King --- .../internal/SessionFactoryImpl.java | 145 +++--------------- .../internal/SessionFactorySettings.java | 129 ++++++++++++++++ .../domain/internal/MappingMetamodelImpl.java | 5 - .../metamodel/spi/MetamodelImplementor.java | 8 +- 4 files changed, 158 insertions(+), 129 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/internal/SessionFactorySettings.java diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index fddb6eb1f4..d1f26ccb53 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -41,7 +41,6 @@ import org.hibernate.StatelessSession; import org.hibernate.StatelessSessionBuilder; import org.hibernate.UnknownFilterException; import org.hibernate.binder.internal.TenantIdBinder; -import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService; import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; @@ -52,15 +51,12 @@ import org.hibernate.cache.cfg.internal.DomainDataRegionConfigImpl; import org.hibernate.cache.cfg.spi.DomainDataRegionConfig; import org.hibernate.cache.spi.CacheImplementor; import org.hibernate.cache.spi.access.AccessType; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.PersistenceSettings; import org.hibernate.context.internal.JTASessionContext; import org.hibernate.context.internal.ManagedSessionContext; import org.hibernate.context.internal.ThreadLocalSessionContext; import org.hibernate.context.spi.CurrentSessionContext; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.profile.FetchProfile; @@ -103,9 +99,6 @@ import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.sql.spi.NativeQueryImplementor; import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.function.SqmFunctionRegistry; -import org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableStrategy; -import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableStrategy; -import org.hibernate.query.sqm.mutation.internal.temptable.PersistentTableStrategy; import org.hibernate.query.sqm.spi.NamedSqmQueryMemento; import org.hibernate.relational.SchemaManager; import org.hibernate.relational.internal.SchemaManagerImpl; @@ -138,14 +131,12 @@ import static jakarta.persistence.SynchronizationType.SYNCHRONIZED; import static java.util.Collections.emptySet; import static java.util.Collections.unmodifiableSet; import static org.hibernate.cfg.AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS; -import static org.hibernate.cfg.AvailableSettings.JAKARTA_VALIDATION_FACTORY; -import static org.hibernate.cfg.AvailableSettings.JPA_VALIDATION_FACTORY; -import static org.hibernate.cfg.PersistenceSettings.PERSISTENCE_UNIT_NAME; -import static org.hibernate.cfg.PersistenceSettings.SESSION_FACTORY_JNDI_NAME; -import static org.hibernate.engine.config.spi.StandardConverters.STRING; import static org.hibernate.internal.FetchProfileHelper.getFetchProfiles; -import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER; -import static org.hibernate.internal.util.StringHelper.isNotEmpty; +import static org.hibernate.internal.SessionFactorySettings.deprecationCheck; +import static org.hibernate.internal.SessionFactorySettings.determineJndiName; +import static org.hibernate.internal.SessionFactorySettings.getSessionFactoryName; +import static org.hibernate.internal.SessionFactorySettings.getSettings; +import static org.hibernate.internal.SessionFactorySettings.maskOutSensitiveInformation; import static org.hibernate.jpa.HibernateHints.HINT_TENANT_ID; import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer; import static org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT; @@ -262,8 +253,8 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im //noinspection unchecked tenantIdentifierJavaType = jdbcMapping.getJavaTypeDescriptor(); } - for (Map.Entry filterEntry : filters.entrySet()) { - if (filterEntry.getValue().isAutoEnabled()) { + for ( Map.Entry filterEntry : filters.entrySet() ) { + if ( filterEntry.getValue().isAutoEnabled() ) { autoEnabledFilters.add( filterEntry.getValue() ); } } @@ -295,7 +286,8 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im // create runtime metamodels (mapping and JPA) final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl(); runtimeMetamodels = runtimeMetamodelsImpl; - final MappingMetamodelImpl mappingMetamodelImpl = new MappingMetamodelImpl( typeConfiguration, serviceRegistry ); + final MappingMetamodelImpl mappingMetamodelImpl = + new MappingMetamodelImpl( typeConfiguration, serviceRegistry ); runtimeMetamodelsImpl.setMappingMetamodel( mappingMetamodelImpl ); fastSessionServices = new FastSessionServices( this ); initializeMappingModel( mappingMetamodelImpl, bootstrapContext, bootMetamodel, options ); @@ -337,38 +329,16 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im LOG.debug( "Instantiated SessionFactory" ); } - private static void deprecationCheck(Map settings) { - for ( String s:settings.keySet() ) { - switch (s) { - case "hibernate.hql.bulk_id_strategy.global_temporary.create_tables": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.global_temporary.create_tables", GlobalTemporaryTableStrategy.CREATE_ID_TABLES ); - case "hibernate.hql.bulk_id_strategy.global_temporary.drop_tables": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.global_temporary.drop_tables", GlobalTemporaryTableStrategy.DROP_ID_TABLES ); - case "hibernate.hql.bulk_id_strategy.persistent.create_tables": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.create_tables", PersistentTableStrategy.CREATE_ID_TABLES ); - case "hibernate.hql.bulk_id_strategy.persistent.drop_tables": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.drop_tables", PersistentTableStrategy.DROP_ID_TABLES ); - case "hibernate.hql.bulk_id_strategy.persistent.schema": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.schema", PersistentTableStrategy.SCHEMA ); - case "hibernate.hql.bulk_id_strategy.persistent.catalog": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.catalog", PersistentTableStrategy.CATALOG ); - case "hibernate.hql.bulk_id_strategy.local_temporary.drop_tables": - DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.local_temporary.drop_tables", LocalTemporaryTableStrategy.DROP_ID_TABLES ); - } - } - } - private void initializeMappingModel( MappingMetamodelImpl mappingMetamodelImpl, BootstrapContext bootstrapContext, MetadataImplementor bootMetamodel, SessionFactoryOptions options) { - final TypeConfiguration typeConfiguration = mappingMetamodelImpl.getTypeConfiguration(); mappingMetamodelImpl.finishInitialization( runtimeModelCreationContext( bootstrapContext, bootMetamodel, mappingMetamodelImpl, - typeConfiguration, + mappingMetamodelImpl.getTypeConfiguration(), options ) ); } @@ -528,58 +498,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im } - private static Map getSettings(SessionFactoryOptions options, SessionFactoryServiceRegistry serviceRegistry) { - final Map settings = serviceRegistry.requireService( ConfigurationService.class ).getSettings(); - final Map result = new HashMap<>( settings ); - if ( !settings.containsKey( JPA_VALIDATION_FACTORY ) && !settings.containsKey( JAKARTA_VALIDATION_FACTORY ) ) { - final Object reference = options.getValidatorFactoryReference(); - if ( reference != null ) { - result.put( JPA_VALIDATION_FACTORY, reference ); - result.put( JAKARTA_VALIDATION_FACTORY, reference ); - } - } - return result; - } - - private static String getSessionFactoryName(SessionFactoryOptions options, SessionFactoryServiceRegistry serviceRegistry) { - final String sessionFactoryName = options.getSessionFactoryName(); - if ( sessionFactoryName != null ) { - return sessionFactoryName; - } - - final CfgXmlAccessService cfgXmlAccessService = serviceRegistry.requireService( CfgXmlAccessService.class ); - if ( cfgXmlAccessService.getAggregatedConfig() != null ) { - final String nameFromAggregation = cfgXmlAccessService.getAggregatedConfig().getSessionFactoryName(); - if ( nameFromAggregation != null ) { - return nameFromAggregation; - } - } - - - final ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class ); - assert configurationService != null; - return configurationService.getSetting( PersistenceSettings.PERSISTENCE_UNIT_NAME, STRING ); - } - - private String determineJndiName( - String name, - SessionFactoryOptions options, - SessionFactoryServiceRegistry serviceRegistry) { - final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); - assert cfgService != null; - final String explicitJndiName = cfgService.getSetting( SESSION_FACTORY_JNDI_NAME, STRING ); - if ( isNotEmpty( explicitJndiName ) ) { - return explicitJndiName; - } - - final String puName = cfgService.getSetting( PERSISTENCE_UNIT_NAME, STRING ); - // do not use name for JNDI if explicitly asked not to or if name comes from JPA persistence-unit name - final boolean nameIsNotJndiName = - options.isSessionFactoryNameAlsoJndiName() == Boolean.FALSE - || isNotEmpty( puName ); - return !nameIsNotJndiName ? name : null; - } - private SessionBuilderImpl createDefaultSessionOpenOptionsIfPossible() { final CurrentTenantIdentifierResolver tenantIdResolver = getCurrentTenantIdentifierResolver(); if ( tenantIdResolver == null ) { @@ -888,7 +806,7 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im LOG.debug( "Returning a Reference to the SessionFactory" ); return new Reference( SessionFactoryImpl.class.getName(), - new StringRefAddr("uuid", getUuid()), + new StringRefAddr( "uuid", getUuid() ), SessionFactoryRegistry.ObjectFactoryImpl.class.getName(), null ); @@ -954,20 +872,16 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im runtimeMetamodels.getMappingMetamodel().forEachEntityDescriptor( entityPersister -> { if ( entityPersister.getSqmMultiTableMutationStrategy() != null ) { - entityPersister.getSqmMultiTableMutationStrategy().release( - this, - jdbcConnectionAccess - ); + entityPersister.getSqmMultiTableMutationStrategy() + .release( this, jdbcConnectionAccess ); } if ( entityPersister.getSqmMultiTableInsertStrategy() != null ) { - entityPersister.getSqmMultiTableInsertStrategy().release( - this, - jdbcConnectionAccess - ); + entityPersister.getSqmMultiTableInsertStrategy() + .release( this, jdbcConnectionAccess ); } } ); - ( (MappingMetamodelImpl) runtimeMetamodels.getMappingMetamodel() ).close(); +// ( (MappingMetamodelImpl) runtimeMetamodels.getMappingMetamodel() ).close(); } if ( queryEngine != null ) { @@ -1324,18 +1238,18 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im // set up default builder values... final SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions(); - this.statementInspector = sessionFactoryOptions.getStatementInspector(); - this.connectionHandlingMode = sessionFactoryOptions.getPhysicalConnectionHandlingMode(); - this.autoClose = sessionFactoryOptions.isAutoCloseSessionEnabled(); - this.defaultBatchFetchSize = sessionFactoryOptions.getDefaultBatchFetchSize(); - this.subselectFetchEnabled = sessionFactoryOptions.isSubselectFetchEnabled(); + statementInspector = sessionFactoryOptions.getStatementInspector(); + connectionHandlingMode = sessionFactoryOptions.getPhysicalConnectionHandlingMode(); + autoClose = sessionFactoryOptions.isAutoCloseSessionEnabled(); + defaultBatchFetchSize = sessionFactoryOptions.getDefaultBatchFetchSize(); + subselectFetchEnabled = sessionFactoryOptions.isSubselectFetchEnabled(); final CurrentTenantIdentifierResolver currentTenantIdentifierResolver = sessionFactory.getCurrentTenantIdentifierResolver(); if ( currentTenantIdentifierResolver != null ) { tenantIdentifier = currentTenantIdentifierResolver.resolveCurrentTenantIdentifier(); } - this.jdbcTimeZone = sessionFactoryOptions.getJdbcTimeZone(); + jdbcTimeZone = sessionFactoryOptions.getJdbcTimeZone(); } @@ -1772,21 +1686,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im return (SessionFactoryImpl) locateSessionFactoryOnDeserialization( uuid, name ); } - private static void maskOutSensitiveInformation(Map props) { - maskOutIfSet( props, AvailableSettings.JPA_JDBC_USER ); - maskOutIfSet( props, AvailableSettings.JPA_JDBC_PASSWORD ); - maskOutIfSet( props, AvailableSettings.JAKARTA_JDBC_USER ); - maskOutIfSet( props, AvailableSettings.JAKARTA_JDBC_PASSWORD ); - maskOutIfSet( props, AvailableSettings.USER ); - maskOutIfSet( props, AvailableSettings.PASS ); - } - - private static void maskOutIfSet(Map props, String setting) { - if ( props.containsKey( setting ) ) { - props.put( setting, "****" ); - } - } - /** * @return the {@link FastSessionServices} for this {@code SessionFactory}. */ diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactorySettings.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactorySettings.java new file mode 100644 index 0000000000..71a7274027 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactorySettings.java @@ -0,0 +1,129 @@ +/* + * 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.internal; + +import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService; +import org.hibernate.boot.cfgxml.spi.LoadedConfig; +import org.hibernate.boot.spi.SessionFactoryOptions; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableStrategy; +import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableStrategy; +import org.hibernate.query.sqm.mutation.internal.temptable.PersistentTableStrategy; +import org.hibernate.service.spi.SessionFactoryServiceRegistry; + +import java.util.HashMap; +import java.util.Map; + +import static org.hibernate.cfg.PersistenceSettings.PERSISTENCE_UNIT_NAME; +import static org.hibernate.cfg.PersistenceSettings.SESSION_FACTORY_JNDI_NAME; +import static org.hibernate.cfg.ValidationSettings.JAKARTA_VALIDATION_FACTORY; +import static org.hibernate.cfg.ValidationSettings.JPA_VALIDATION_FACTORY; +import static org.hibernate.engine.config.spi.StandardConverters.STRING; +import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER; +import static org.hibernate.internal.util.StringHelper.isNotEmpty; + +/** + * Helper methods used to set up a {@link SessionFactoryImpl}. + */ +class SessionFactorySettings { + + static Map getSettings( + SessionFactoryOptions options, SessionFactoryServiceRegistry serviceRegistry) { + final Map settings = + serviceRegistry.requireService( ConfigurationService.class ) + .getSettings(); + final Map result = new HashMap<>( settings ); + if ( !settings.containsKey( JPA_VALIDATION_FACTORY ) + && !settings.containsKey( JAKARTA_VALIDATION_FACTORY ) ) { + final Object reference = options.getValidatorFactoryReference(); + if ( reference != null ) { + result.put( JPA_VALIDATION_FACTORY, reference ); + result.put( JAKARTA_VALIDATION_FACTORY, reference ); + } + } + return result; + } + + static String getSessionFactoryName( + SessionFactoryOptions options, SessionFactoryServiceRegistry serviceRegistry) { + final String sessionFactoryName = options.getSessionFactoryName(); + if ( sessionFactoryName != null ) { + return sessionFactoryName; + } + + final LoadedConfig loadedConfig = + serviceRegistry.requireService( CfgXmlAccessService.class ) + .getAggregatedConfig(); + if ( loadedConfig != null ) { + final String nameFromAggregation = loadedConfig.getSessionFactoryName(); + if ( nameFromAggregation != null ) { + return nameFromAggregation; + } + } + + final ConfigurationService configurationService = + serviceRegistry.requireService( ConfigurationService.class ); + return configurationService.getSetting( PERSISTENCE_UNIT_NAME, STRING ); + } + + static void maskOutSensitiveInformation(Map props) { + maskOutIfSet( props, AvailableSettings.JPA_JDBC_USER ); + maskOutIfSet( props, AvailableSettings.JPA_JDBC_PASSWORD ); + maskOutIfSet( props, AvailableSettings.JAKARTA_JDBC_USER ); + maskOutIfSet( props, AvailableSettings.JAKARTA_JDBC_PASSWORD ); + maskOutIfSet( props, AvailableSettings.USER ); + maskOutIfSet( props, AvailableSettings.PASS ); + } + + private static void maskOutIfSet(Map props, String setting) { + if ( props.containsKey( setting ) ) { + props.put( setting, "****" ); + } + } + + static String determineJndiName( + String name, + SessionFactoryOptions options, + SessionFactoryServiceRegistry serviceRegistry) { + final ConfigurationService configService = + serviceRegistry.requireService( ConfigurationService.class ); + final String explicitJndiName = configService.getSetting( SESSION_FACTORY_JNDI_NAME, STRING ); + if ( isNotEmpty( explicitJndiName ) ) { + return explicitJndiName; + } + else { + final String unitName = configService.getSetting( PERSISTENCE_UNIT_NAME, STRING ); + // do not use name for JNDI if explicitly asked not to or if name comes from JPA persistence-unit name + final boolean nameIsNotJndiName = + options.isSessionFactoryNameAlsoJndiName() == Boolean.FALSE + || isNotEmpty( unitName ); + return !nameIsNotJndiName ? name : null; + } + } + + static void deprecationCheck(Map settings) { + for ( String setting:settings.keySet() ) { + switch ( setting ) { + case "hibernate.hql.bulk_id_strategy.global_temporary.create_tables": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.global_temporary.create_tables", GlobalTemporaryTableStrategy.CREATE_ID_TABLES ); + case "hibernate.hql.bulk_id_strategy.global_temporary.drop_tables": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.global_temporary.drop_tables", GlobalTemporaryTableStrategy.DROP_ID_TABLES ); + case "hibernate.hql.bulk_id_strategy.persistent.create_tables": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.create_tables", PersistentTableStrategy.CREATE_ID_TABLES ); + case "hibernate.hql.bulk_id_strategy.persistent.drop_tables": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.drop_tables", PersistentTableStrategy.DROP_ID_TABLES ); + case "hibernate.hql.bulk_id_strategy.persistent.schema": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.schema", PersistentTableStrategy.SCHEMA ); + case "hibernate.hql.bulk_id_strategy.persistent.catalog": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.persistent.catalog", PersistentTableStrategy.CATALOG ); + case "hibernate.hql.bulk_id_strategy.local_temporary.drop_tables": + DEPRECATION_LOGGER.deprecatedSetting( "hibernate.hql.bulk_id_strategy.local_temporary.drop_tables", LocalTemporaryTableStrategy.DROP_ID_TABLES ); + } + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java index 5ecd47d1c7..13a8b31113 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java @@ -748,11 +748,6 @@ public class MappingMetamodelImpl extends QueryParameterBindingTypeResolverImpl return null; } - @Override - public void close() { - // anything to do ? - } - private String[] doGetImplementors(Class clazz) throws MappingException { final ArrayList results = new ArrayList<>(); for ( EntityPersister checkPersister : entityPersisters().values() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java index 0cfcbd6dd0..ae65e82efa 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java @@ -132,5 +132,11 @@ public interface MetamodelImplementor extends MappingMetamodel, Metamodel { */ String[] getAllCollectionRoles(); - void close(); + /** + * @deprecated No longer called + */ + @Deprecated(since = "7", forRemoval = true) + default void close() { + // nothing to do + } }