Reputation: 557
This is follow up of this Multitenancy with Spring JPA
I chose to use the 'AbstractRoutingDataSource'. But the problem is now datasource and entitymanager bean initialized at startup. is there anyway to configure this in spring which way that it will initialize after user is authenticated?
Another problem which i can think of will be how to handle concurrency. I put tenantId in this class
public class ThreadLocalContextUtil {
private static final ThreadLocal<String> contextHolder =
new ThreadLocal<String>();
public static void setTenantId(String tenantId) {
Assert.notNull(tenantId, "customerType cannot be null");
contextHolder.set(tenantId);
}
public static String getTenantId() {
return (String) contextHolder.get();
}
public static void clearTenant() {
contextHolder.remove();
}
}
The solution i can think of will be to remove the tenantId after the datasource is initialized. is that correct?
Upvotes: 3
Views: 3837
Reputation: 1130
I've solved similar problem. I implemented my own TenantAwareDataSource
based on Spring's AbstractDataSource
. It takes tenantId from a session-scoped bean named tenantContext. This bean is updated every time the incoming request is processed. It is done by using Spring Security's security filter:
<security:http auto-config='false' >
<security:custom-filter before="FIRST" ref="tenantFilter" />
<!-- ...more security stuff... -->
</security:http>
My TenantAwareDataSource
is initialized in the startup time, but it does not matter because it is created empty - it contains no tenant datasources (e.g. pooled JDBC datasources or JPA entity manager). They are created lazily when the getConnection()
is called for the first time for the selected tenant.
So, my TenantAwareDataSource
maintains its own dynamic datasource map while AbstractRoutingDataSource
expects static initializitaion of the datasource map done in the startup time.
Read more detailed description in this article.
Upvotes: 3