Skip to content

Commit

Permalink
Deprecate quarkus.hibernate-orm.multitenant-schema-datasource
Browse files Browse the repository at this point in the history
There are no tests involving this configuration property.

`quarkus.hibernate-orm.datasource` serves the exact same purpose and is
more standardized and better handled (e.g. in Dev UI).

See #18564
  • Loading branch information
yrodiere committed May 16, 2024
1 parent 28c7fed commit a1a0ecd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/hibernate-orm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ quarkus.hibernate-orm.database.generation=none
# Enable SCHEMA approach and use default datasource
quarkus.hibernate-orm.multitenant=SCHEMA
# You could use a non-default datasource by using the following setting
# quarkus.hibernate-orm.multitenant-schema-datasource=other
# quarkus.hibernate-orm.datasource=other
# The default data source used for all tenant schemas
quarkus.datasource.db-kind=postgresql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ public interface HibernateOrmConfigPersistenceUnit {
/**
* Defines the name of the datasource to use in case of SCHEMA approach. The datasource of the persistence unit will be used
* if not set.
*
* @deprecated Use {@link #datasource()} instead.
*/
@Deprecated
@WithConverter(TrimmedStringConverter.class)
Optional<String> multitenantSchemaDatasource();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,27 @@ public void multitenancy(HibernateOrmRecorder recorder,
boolean multitenancyEnabled = false;

for (PersistenceUnitDescriptorBuildItem persistenceUnitDescriptor : persistenceUnitDescriptors) {
switch (persistenceUnitDescriptor.getConfig().getMultiTenancyStrategy()) {
String persistenceUnitConfigName = persistenceUnitDescriptor.getConfigurationName();
var multitenancyStrategy = persistenceUnitDescriptor.getConfig().getMultiTenancyStrategy();
switch (multitenancyStrategy) {
case NONE -> {
}
case DISCRIMINATOR -> multitenancyEnabled = true;
case DATABASE, SCHEMA -> {
multitenancyEnabled = true;

String multiTenancySchemaDataSource = persistenceUnitDescriptor.getMultiTenancySchemaDataSource();
Optional<String> datasource;
if (multitenancyStrategy == MultiTenancyStrategy.SCHEMA && multiTenancySchemaDataSource != null) {
LOG.warnf("Configuration property '%1$s' is deprecated. Use '%2$s' instead.",
HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitConfigName,
"multitenant-schema-datasource"),
HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitConfigName, "datasource"));
datasource = Optional.of(multiTenancySchemaDataSource);
} else {
datasource = persistenceUnitDescriptor.getConfig().getDataSource();
}

ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem
.configure(DataSourceTenantConnectionResolver.class)
.scope(ApplicationScoped.class)
Expand All @@ -685,9 +700,8 @@ public void multitenancy(HibernateOrmRecorder recorder,
.unremovable()
.supplier(recorder.dataSourceTenantConnectionResolver(
persistenceUnitDescriptor.getPersistenceUnitName(),
persistenceUnitDescriptor.getConfig().getDataSource(),
persistenceUnitDescriptor.getConfig().getMultiTenancyStrategy(),
persistenceUnitDescriptor.getMultiTenancySchemaDataSource()));
datasource,
persistenceUnitDescriptor.getConfig().getMultiTenancyStrategy()));

if (PersistenceUnitUtil.isDefaultPersistenceUnit(persistenceUnitDescriptor.getPersistenceUnitName())) {
configurator.addQualifier(Default.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ public void created(BeanContainer beanContainer) {

public Supplier<DataSourceTenantConnectionResolver> dataSourceTenantConnectionResolver(String persistenceUnitName,
Optional<String> dataSourceName,
MultiTenancyStrategy multiTenancyStrategy, String multiTenancySchemaDataSourceName) {
MultiTenancyStrategy multiTenancyStrategy) {
return new Supplier<DataSourceTenantConnectionResolver>() {
@Override
public DataSourceTenantConnectionResolver get() {
return new DataSourceTenantConnectionResolver(persistenceUnitName, dataSourceName, multiTenancyStrategy,
multiTenancySchemaDataSourceName);
return new DataSourceTenantConnectionResolver(persistenceUnitName, dataSourceName, multiTenancyStrategy);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,22 @@ public class DataSourceTenantConnectionResolver implements TenantConnectionResol

private MultiTenancyStrategy multiTenancyStrategy;

private String multiTenancySchemaDataSourceName;

public DataSourceTenantConnectionResolver() {
}

public DataSourceTenantConnectionResolver(String persistenceUnitName, Optional<String> dataSourceName,
MultiTenancyStrategy multiTenancyStrategy, String multiTenancySchemaDataSourceName) {
MultiTenancyStrategy multiTenancyStrategy) {
this.persistenceUnitName = persistenceUnitName;
this.dataSourceName = dataSourceName;
this.multiTenancyStrategy = multiTenancyStrategy;
this.multiTenancySchemaDataSourceName = multiTenancySchemaDataSourceName;
}

@Override
public ConnectionProvider resolve(String tenantId) {
LOG.debugv("resolve((persistenceUnitName={0}, tenantIdentifier={1})", persistenceUnitName, tenantId);
LOG.debugv("multitenancy strategy: {0}", multiTenancyStrategy);

AgroalDataSource dataSource = tenantDataSource(dataSourceName, tenantId, multiTenancyStrategy,
multiTenancySchemaDataSourceName);
AgroalDataSource dataSource = tenantDataSource(dataSourceName, tenantId, multiTenancyStrategy);
if (dataSource == null) {
throw new IllegalStateException(
String.format(Locale.ROOT, "No instance of datasource found for persistence unit '%1$s' and tenant '%2$s'",
Expand All @@ -67,17 +63,12 @@ public ConnectionProvider resolve(String tenantId) {
}

private static AgroalDataSource tenantDataSource(Optional<String> dataSourceName, String tenantId,
MultiTenancyStrategy strategy, String multiTenancySchemaDataSourceName) {
MultiTenancyStrategy strategy) {
return switch (strategy) {
case DATABASE -> Arc.container().instance(AgroalDataSource.class, new DataSource.DataSourceLiteral(tenantId)).get();
case SCHEMA -> {
if (multiTenancySchemaDataSourceName == null) {
// The datasource name should always be present when using a multi-tenancy other than DATABASE;
// we perform checks in HibernateOrmProcessor during the build.
yield getDataSource(dataSourceName.get());
}
yield getDataSource(multiTenancySchemaDataSourceName);
}
// The datasource name should always be present when using a multi-tenancy other than DATABASE;
// we perform checks in HibernateOrmProcessor during the build.
case SCHEMA -> getDataSource(dataSourceName.get());
default -> throw new IllegalStateException("Unexpected multitenancy strategy: " + strategy);
};
}
Expand Down

0 comments on commit a1a0ecd

Please sign in to comment.