The routing can be done by Hibernate via its MultiTenancyConnectionProvider
which you can implement like this:
public class MultiTenantConnectionProvider
extends AbstractMultiTenantConnectionProvider {
public static final MultiTenantConnectionProvider INSTANCE =
new MultiTenantConnectionProvider();
private final Map<String, ConnectionProvider> connectionProviderMap =
new HashMap<>();
Map<String, ConnectionProvider> getConnectionProviderMap() {
return connectionProviderMap;
}
@Override
protected ConnectionProvider getAnyConnectionProvider() {
return connectionProviderMap.get(
TenantContext.DEFAULT_TENANT_IDENTIFIER
);
}
@Override
protected ConnectionProvider selectConnectionProvider(
String tenantIdentifier) {
return connectionProviderMap.get(
tenantIdentifier
);
}
}
Assuming each tenant uses its own dedicated DataSource
, you can register the individual ConnectionProvider
(s) using a utility method like this one:
private void addTenantConnectionProvider(
String tenantId,
DataSource tenantDataSource,
Properties properties) {
DatasourceConnectionProviderImpl connectionProvider =
new DatasourceConnectionProviderImpl();
connectionProvider.setDataSource(tenantDataSource);
connectionProvider.configure(properties);
MultiTenantConnectionProvider.INSTANCE
.getConnectionProviderMap()
.put(
tenantId,
connectionProvider
);
}
You can register a default tenant for admin-related tasks:
addTenantConnectionProvider(
TenantContext.DEFAULT_TENANT_IDENTIFIER,
defaultDataSource,
properties()
);
And for the actual tenants, you could use a method like this one:
private void addTenantConnectionProvider(
String tenantId) {
DataSourceProvider dataSourceProvider = database()
.dataSourceProvider();
Properties properties = properties();
MysqlDataSource tenantDataSource = new MysqlDataSource();
tenantDataSource.setDatabaseName(tenantId);
tenantDataSource.setUser(dataSourceProvider.username());
tenantDataSource.setPassword(dataSourceProvider.password());
properties.put(
Environment.DATASOURCE,
dataSourceProxyType().dataSource(tenantDataSource)
);
addTenantConnectionProvider(
tenantId,
tenantDataSource,
properties
);
}
You can change the MysqlDataSource
to whatever database you are using.
Then, registering the tenants is as simple as that:
addTenantConnectionProvider("asia");
addTenantConnectionProvider("europe");
The last thing to take into consideration is to provide the MultiTenancyConnectionProvider
implementation to Hibernate via the hibernate.multi_tenant_connection_provider
configuration property.
properties.put(
AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER,
MultiTenantConnectionProvider.INSTANCE
);
addTenantConnectionProvider("asia");
I may know all the tenants upfront. – Cryptic