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?