HHH-16310 Centralize the detection of multitenancy

This commit is contained in:
Yoann Rodière 2023-03-15 10:50:19 +01:00
parent 5bff5a383c
commit 9c1d455c8a
6 changed files with 15 additions and 16 deletions

View File

@ -64,7 +64,7 @@ import org.hibernate.cfg.MetadataSourceType;
import org.hibernate.dialect.TimeZoneSupport; import org.hibernate.dialect.TimeZoneSupport;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
@ -619,7 +619,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
this.mappingDefaults = new MappingDefaultsImpl( serviceRegistry ); this.mappingDefaults = new MappingDefaultsImpl( serviceRegistry );
this.defaultTimezoneStorage = resolveTimeZoneStorageStrategy( configService ); this.defaultTimezoneStorage = resolveTimeZoneStorageStrategy( configService );
this.multiTenancyEnabled = serviceRegistry.getService(MultiTenantConnectionProvider.class)!=null; this.multiTenancyEnabled = JdbcEnvironmentImpl.isMultiTenancyEnabled( serviceRegistry );
this.xmlMappingEnabled = configService.getSetting( this.xmlMappingEnabled = configService.getSetting(
AvailableSettings.XML_MAPPING_ENABLED, AvailableSettings.XML_MAPPING_ENABLED,

View File

@ -41,6 +41,7 @@ import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.id.uuid.LocalObjectUuidHelper; import org.hibernate.id.uuid.LocalObjectUuidHelper;
@ -352,7 +353,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
this.checkNullability = configurationService.getSetting( CHECK_NULLABILITY, BOOLEAN, true ); this.checkNullability = configurationService.getSetting( CHECK_NULLABILITY, BOOLEAN, true );
this.initializeLazyStateOutsideTransactions = configurationService.getSetting( ENABLE_LAZY_LOAD_NO_TRANS, BOOLEAN, false ); this.initializeLazyStateOutsideTransactions = configurationService.getSetting( ENABLE_LAZY_LOAD_NO_TRANS, BOOLEAN, false );
this.multiTenancyEnabled = serviceRegistry.getService(MultiTenantConnectionProvider.class)!=null; this.multiTenancyEnabled = JdbcEnvironmentImpl.isMultiTenancyEnabled( serviceRegistry );
this.currentTenantIdentifierResolver = strategySelector.resolveStrategy( this.currentTenantIdentifierResolver = strategySelector.resolveStrategy(
CurrentTenantIdentifierResolver.class, CurrentTenantIdentifierResolver.class,
configurationSettings.get( MULTI_TENANT_IDENTIFIER_RESOLVER ) configurationSettings.get( MULTI_TENANT_IDENTIFIER_RESOLVER )

View File

@ -22,6 +22,7 @@ import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.internal.log.DeprecationLogger;
@ -94,7 +95,7 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator<Con
@Override @Override
public ConnectionProvider initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) { public ConnectionProvider initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) {
if ( registry.getService( MultiTenantConnectionProvider.class ) != null ) { if ( JdbcEnvironmentImpl.isMultiTenancyEnabled( registry ) ) {
// nothing to do, but given the separate hierarchies have to handle this here. // nothing to do, but given the separate hierarchies have to handle this here.
return null; return null;
} }

View File

@ -16,6 +16,7 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper; import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder; import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
@ -42,6 +43,10 @@ import org.jboss.logging.Logger;
public class JdbcEnvironmentImpl implements JdbcEnvironment { public class JdbcEnvironmentImpl implements JdbcEnvironment {
private static final Logger log = Logger.getLogger( JdbcEnvironmentImpl.class ); private static final Logger log = Logger.getLogger( JdbcEnvironmentImpl.class );
public static boolean isMultiTenancyEnabled(ServiceRegistry serviceRegistry) {
return serviceRegistry.getService( MultiTenantConnectionProvider.class ) != null;
}
private final Dialect dialect; private final Dialect dialect;
private final SqlAstTranslatorFactory sqlAstTranslatorFactory; private final SqlAstTranslatorFactory sqlAstTranslatorFactory;

View File

@ -279,8 +279,7 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
} }
private JdbcConnectionAccess buildJdbcConnectionAccess(ServiceRegistryImplementor registry) { private JdbcConnectionAccess buildJdbcConnectionAccess(ServiceRegistryImplementor registry) {
boolean multiTenancyEnabled = registry.getService( MultiTenantConnectionProvider.class ) != null; if ( !JdbcEnvironmentImpl.isMultiTenancyEnabled( registry ) ) {
if ( !multiTenancyEnabled ) {
ConnectionProvider connectionProvider = registry.getService( ConnectionProvider.class ); ConnectionProvider connectionProvider = registry.getService( ConnectionProvider.class );
return new ConnectionProviderJdbcConnectionAccess( connectionProvider ); return new ConnectionProviderJdbcConnectionAccess( connectionProvider );
} }
@ -290,10 +289,8 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
} }
} }
public static JdbcConnectionAccess buildBootstrapJdbcConnectionAccess( public static JdbcConnectionAccess buildBootstrapJdbcConnectionAccess(ServiceRegistryImplementor registry) {
boolean multiTenancyEnabled, if ( !JdbcEnvironmentImpl.isMultiTenancyEnabled( registry ) ) {
ServiceRegistryImplementor registry) {
if ( !multiTenancyEnabled ) {
ConnectionProvider connectionProvider = registry.getService( ConnectionProvider.class ); ConnectionProvider connectionProvider = registry.getService( ConnectionProvider.class );
return new ConnectionProviderJdbcConnectionAccess( connectionProvider ); return new ConnectionProviderJdbcConnectionAccess( connectionProvider );
} }

View File

@ -13,7 +13,6 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator; import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator; import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
@ -34,8 +33,6 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
private ServiceRegistryImplementor serviceRegistry; private ServiceRegistryImplementor serviceRegistry;
private JdbcEnvironment jdbcEnvironment; private JdbcEnvironment jdbcEnvironment;
private boolean multiTenancyEnabled;
private SqlStatementLogger sqlStatementLogger; private SqlStatementLogger sqlStatementLogger;
@Override @Override
@ -48,8 +45,6 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
this.jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class ); this.jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
assert jdbcEnvironment != null : "JdbcEnvironment was not found"; assert jdbcEnvironment != null : "JdbcEnvironment was not found";
this.multiTenancyEnabled = serviceRegistry.getService( MultiTenantConnectionProvider.class ) != null;
final boolean showSQL = ConfigurationHelper.getBoolean( Environment.SHOW_SQL, configValues, false ); final boolean showSQL = ConfigurationHelper.getBoolean( Environment.SHOW_SQL, configValues, false );
final boolean formatSQL = ConfigurationHelper.getBoolean( Environment.FORMAT_SQL, configValues, false ); final boolean formatSQL = ConfigurationHelper.getBoolean( Environment.FORMAT_SQL, configValues, false );
final boolean highlightSQL = ConfigurationHelper.getBoolean( Environment.HIGHLIGHT_SQL, configValues, false ); final boolean highlightSQL = ConfigurationHelper.getBoolean( Environment.HIGHLIGHT_SQL, configValues, false );
@ -65,7 +60,7 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
@Override @Override
public JdbcConnectionAccess getBootstrapJdbcConnectionAccess() { public JdbcConnectionAccess getBootstrapJdbcConnectionAccess() {
return JdbcEnvironmentInitiator.buildBootstrapJdbcConnectionAccess( multiTenancyEnabled, serviceRegistry ); return JdbcEnvironmentInitiator.buildBootstrapJdbcConnectionAccess( serviceRegistry );
} }
@Override @Override