I want to know how hibernate's multi-tenant connection provider handles the data base connections for multiple tenants in separate database multi-tenancy approach. If I hit the API with same tenant, say 1234, then at first hit it should CONNECT to that tenant's specific database and after several hits for same tenant, will it use the same connection of database or it will open the new connection again for the same tenant?
I have used AbstractDataSourceBasedMultiTenantConnectionProviderImpl
and CurrentTenantIdentifierResolver
implementations in database per tenant approach
class CurrentTenantIdentifierResolver {
// Other methods + fields
public String resolveCurrentTenantIdentifier() {
String tenantId = TenantContext.getCurrentTenant();
return tenantId;
}
}
class AbstractDataSourceBasedMultiTenantConnectionProviderImpl {
// Other methods + fields
@Override protected DataSource selectDataSource(String tenantIdentifier) {
return MultiTenantInitService.getDataSourceMap().get(tenantIdentifier);
}
}
class MultiTenantInitService {
// Other methods + fields
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(prop.getProperty("spring.jpa.properties.hibernate.driver-class-name"));
dataSource.setUrl(prop.getProperty("spring.datasource.url"));
dataSource.setUsername(prop.getProperty("spring.datasource.username"));
dataSource.setPassword(prop.getProperty("spring.datasource.password"));
dataSourceMap.put(ApplicationConstants.DEFAULT_TENANT_ID, dataSource);
}
I expected multi tenant connection provider to connect to the database on first hit of API for single tenant for once only. It should not open the connections again and again for the same tenant. The new connection with data base should only be formed for new tenant. But, if it does open the connections then it should also manage the connection closing on its own.