'Specifying tenant using SessionBuilder.tenantIdentifier() vs CurrentTenantIdentifierResolver - when to use one vs the other?
I'm reading the docs on multi-tenancy in Hibernate and there seem to be two ways that you can specify a tenant identifier.
The first way is by calling SessionBuilder.tenantIdentifier() every time you open a session:
Session session = sessionFactory.withOptions()
.tenantIdentifier(yourTenantIdentifier)
...
.openSession();
The second way is by specifying a custom CurrentTenantIdentifierResolver, by either passing it to the Configuration via its setCurrentTenantIdentifierResolver() method, or by using the hibernate.tenant_identifier_resolver setting:
public class MyCustomResolver implements CurrentTenantIdentifierResolver {
@Override
public String resolveCurrentTenantIdentifier() {
// TODO implement
}
@Override
public boolean validateExistingCurrentSessions() {
return false;
}
}
My question is, when should we use one approach vs the other? What is the advantage/disadvantage of each approach? Also if you use one, should you avoid using the other or can they both be used at the same time?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
