Is it possible to configure Spring Boot to use a MultiTenantConnectionProvider so that each client of my system connects to their own private database?
Specifically I am looking to use the built-in hibernate support for multi-tenancy:
And this is an example of the sort of config I am after, but I can't figure out how to use this in a Spring Boot setup:
I've tried adding these properties to application.properties
:
spring.jpa.hibernate.multiTenancy=DATABASE
spring.jpa.hibernate.tenant_identifier_resolver=com.mystuff.MyCurrentTenantIdentifierResolver
spring.jpa.hibernate.multi_tenant_connection_provider=com.mystuff.MyMultiTenantConnectionProviderImplX
I've also tried coding up my own CurrentTenantIdentifierResolver
and MultiTenantConnectionProvider
and tried serving these up from my main @Configuration bean:
@Bean
public CurrentTenantIdentifierResolver currentTenantIdentifierResolver() {
return new CurrentTenantIdentifierResolver() {
public String resolveCurrentTenantIdentifier() {
// this is never called ...
}
public boolean validateExistingCurrentSessions() {
// this is never called ...
}
};
}
@Bean
public MultiTenantConnectionProvider multiTenantConnectionProvider() {
return new AbstractMultiTenantConnectionProvider() {
protected ConnectionProvider getAnyConnectionProvider() {
// this is never called ...
}
protected ConnectionProvider selectConnectionProvider(String s) {
// this is never called ...
}
};
}
None of this seems to have any affect so my question is really how to get spring-boot / spring-data to use these multi-tenant classes?
Thanks for your help!
LocalContainerEntityManagerFactoryBean
and it works.<br/> But I'm not entirely understand the difference between this and.yml
or.properties
.<br/> I agree with @M.Deinum , inyml
hibernate controls the lifecycle ofmulitTenantConnectionProvider
andCurrentTenantIdentifierResolver
.But I don't know why. – linghu