From a59f2ac6c8ef3d8788a3824a5dc87c07b2724d2a Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 30 Aug 2024 19:05:58 +0200 Subject: [PATCH] some cleanup to DatasourceConnectionProviderImpl and multi-tenant version Signed-off-by: Gavin King --- .../java/org/hibernate/cfg/JdbcSettings.java | 5 +- .../DatasourceConnectionProviderImpl.java | 23 +++++---- ...asedMultiTenantConnectionProviderImpl.java | 51 +++++++++---------- .../internal/JdbcEnvironmentInitiator.java | 7 +-- 4 files changed, 44 insertions(+), 42 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/JdbcSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/JdbcSettings.java index 8f0f628a9d..4aa64ef79a 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/JdbcSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/JdbcSettings.java @@ -526,8 +526,9 @@ public interface JdbcSettings extends C3p0Settings, ProxoolSettings, AgroalSetti /** * @see javax.sql.DataSource * - * @deprecated The JPA-standard {@link #JAKARTA_JTA_DATASOURCE} or {@link #JAKARTA_JTA_DATASOURCE} setting - * is now preferred. + * @deprecated The JPA-standard {@value #JAKARTA_JTA_DATASOURCE} or + * {@value #JAKARTA_NON_JTA_DATASOURCE} setting are now + * preferred. */ @Deprecated String DATASOURCE = "hibernate.connection.datasource"; diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java index acbc35e6fd..6f34d0b703 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java @@ -12,7 +12,8 @@ import java.util.Map; import javax.sql.DataSource; import org.hibernate.HibernateException; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.JdbcSettings; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo; @@ -22,13 +23,17 @@ import org.hibernate.service.spi.Configurable; import org.hibernate.service.spi.InjectService; import org.hibernate.service.spi.Stoppable; +import static org.hibernate.cfg.JdbcSettings.DATASOURCE; + /** * A {@link ConnectionProvider} that manages connections from an underlying {@link DataSource}. *

* The {@link DataSource} to use may be specified by either:

* * @author Gavin King @@ -83,7 +88,7 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con @Override public void configure(Map configValues) { if ( dataSource == null ) { - final Object dataSourceSetting = configValues.get( Environment.DATASOURCE ); + final Object dataSourceSetting = configValues.get( DATASOURCE ); if ( dataSourceSetting instanceof DataSource ) { dataSource = (DataSource) dataSourceSetting; } @@ -91,7 +96,7 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con final String dataSourceJndiName = (String) dataSourceSetting; if ( dataSourceJndiName == null ) { throw new HibernateException( - "DataSource to use was not injected nor specified by [" + Environment.DATASOURCE + "DataSource to use was not injected nor specified by [" + DATASOURCE + "] configuration property" ); } @@ -106,8 +111,8 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con throw new HibernateException( "Unable to determine appropriate DataSource to use" ); } - user = (String) configValues.get( Environment.USER ); - pass = (String) configValues.get( Environment.PASS ); + user = (String) configValues.get( AvailableSettings.USER ); + pass = (String) configValues.get( AvailableSettings.PASS ); useCredentials = user != null || pass != null; available = true; } @@ -150,7 +155,7 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con @Override public String toInfoString() { return dataSourceJndiName != null - ? "\tDatasource JND name [" + dataSourceJndiName + "]" + ? "\tDatasource JNDI name [" + dataSourceJndiName + "]" : "\tProvided DataSource"; } }; diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DataSourceBasedMultiTenantConnectionProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DataSourceBasedMultiTenantConnectionProviderImpl.java index 64b544b5ff..519b9b85e6 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DataSourceBasedMultiTenantConnectionProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DataSourceBasedMultiTenantConnectionProviderImpl.java @@ -22,19 +22,20 @@ import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.Stoppable; +import static org.hibernate.cfg.JdbcSettings.DATASOURCE; import static org.hibernate.cfg.MultiTenancySettings.TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY; /** - * A concrete implementation of the {@link MultiTenantConnectionProvider} contract bases on a number of - * reasonable assumptions. We assume that:
    + * A concrete implementation of the {@link MultiTenantConnectionProvider} contract bases on + * a number of reasonable assumptions. We assume that:
      *
    • - * The {@link DataSource} instances are all available from JNDI named by the tenant identifier relative - * to a single base JNDI context + * The {@link DataSource} instances are all available from JNDI named by the tenant + * identifier relative to a single base JNDI context. *
    • *
    • * {@value AvailableSettings#DATASOURCE} is a string naming either the {@literal any} - * data source or the base JNDI context. If the latter, {@link MultiTenancySettings#TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY} must - * also be set. + * data source or the base JNDI context. If the latter, + * {@link MultiTenancySettings#TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY} must also be set. *
    • *
    * @@ -44,10 +45,11 @@ public class DataSourceBasedMultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl implements ServiceRegistryAwareService, Stoppable { - private Map dataSourceMap; + private final Map dataSourceMap = new ConcurrentHashMap<>(); private JndiService jndiService; private T tenantIdentifierForAny; private String baseJndiNamespace; + private String jndiName; @Override protected DataSource selectAnyDataSource() { @@ -65,21 +67,17 @@ public class DataSourceBasedMultiTenantConnectionProviderImpl } private Map dataSourceMap() { - if ( dataSourceMap == null ) { - dataSourceMap = new ConcurrentHashMap<>(); - } return dataSourceMap; } @Override public void injectServices(ServiceRegistryImplementor serviceRegistry) { - final Object dataSourceConfigValue = - serviceRegistry.requireService( ConfigurationService.class ) - .getSettings().get( AvailableSettings.DATASOURCE ); + final ConfigurationService configurationService = serviceRegistry.requireService( ConfigurationService.class ); + final Object dataSourceConfigValue = configurationService.getSettings().get( DATASOURCE ); if ( !(dataSourceConfigValue instanceof String) ) { - throw new HibernateException( "Improper set up of DataSourceBasedMultiTenantConnectionProviderImpl" ); + throw new HibernateException( "illegal value for configuration setting '" + DATASOURCE + "'" ); } - final String jndiName = (String) dataSourceConfigValue; + jndiName = (String) dataSourceConfigValue; jndiService = serviceRegistry.getService( JndiService.class ); if ( jndiService == null ) { @@ -91,17 +89,18 @@ public class DataSourceBasedMultiTenantConnectionProviderImpl throw new HibernateException( "JNDI name [" + jndiName + "] could not be resolved" ); } - if ( namedObject instanceof DataSource ) { + if ( namedObject instanceof DataSource datasource ) { final int loc = jndiName.lastIndexOf( '/' ); - this.baseJndiNamespace = jndiName.substring( 0, loc ); - this.tenantIdentifierForAny = (T) jndiName.substring( loc + 1 ); - dataSourceMap().put( tenantIdentifierForAny, (DataSource) namedObject ); + baseJndiNamespace = jndiName.substring( 0, loc ); + final String prefix = jndiName.substring(loc + 1); + tenantIdentifierForAny = (T) prefix; + dataSourceMap().put( tenantIdentifierForAny, datasource ); } else if ( namedObject instanceof Context ) { - this.baseJndiNamespace = jndiName; - this.tenantIdentifierForAny = (T) serviceRegistry.requireService( ConfigurationService.class ) - .getSettings() - .get( TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY ); + baseJndiNamespace = jndiName; + final Object configuredTenantId = + configurationService.getSettings().get( TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY ); + tenantIdentifierForAny = (T) configuredTenantId; if ( tenantIdentifierForAny == null ) { throw new HibernateException( "JNDI name named a Context, but tenant identifier to use for ANY was not specified" ); } @@ -116,10 +115,7 @@ public class DataSourceBasedMultiTenantConnectionProviderImpl @Override public void stop() { - if ( dataSourceMap != null ) { - dataSourceMap.clear(); - dataSourceMap = null; - } + dataSourceMap.clear(); } @Override @@ -139,5 +135,4 @@ public class DataSourceBasedMultiTenantConnectionProviderImpl } }; } - } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java index fbf62a8934..b28c3b8714 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java @@ -163,11 +163,12 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator mcp = registry.requireService( MultiTenantConnectionProvider.class ); + final MultiTenantConnectionProvider mcp = + registry.requireService( MultiTenantConnectionProvider.class ); return mcp.getDatabaseConnectionInfo( dialect ); } }