Datasource Decorator For Multi-Tenancy
musabqamri123 opened this issue · 7 comments
When we have a map of data sources in case of multi-tenant, every entry needs to be decorated in order to get it instumented.
How can we configure such thing ?
`else if (bean instanceof Map ) {
Map<?,?> dsMap = (Map<?,?>)bean;
boolean isDatasource = dsMap.values().stream().anyMatch(v -> v instanceof DataSource);
if(isDatasource) {
Map<Object, DataSource> dataSourceMap = new HashMap<>();
dsMap.forEach((key, value) -> {
DataSource ds = (DataSource) value;
if (!ScopedProxyUtils.isScopedTarget(beanName)
&& !getDataSourceDecoratorProperties().getExcludeBeans().contains(beanName)) {
dataSourceMap.put(key, (DataSource) decorateDataSource(ds, beanName));
}
});
return dataSourceMap;
}
}`
Adding this to else part and while creating db connection will use it from the map.
Hi @musabqamri123, can you please provide more information about your use case?
Are you using AbstractRoutingDataSource
with multiple datasources? Is it possible to define DataSource
(s) as separate beans instead of creating Map<?, DataSource>
bean?
We are using similar to it... hibernate’s MultiTenantConnectionProvider.
It’s not possible to create beans for each datasource. As we have around 100 tenants ... and it would limit the onboarding of new one without code change
@musabqamri123 but the Map itself is a spring bean, right? I suppose that you have some code that creates all those datasources and puts them inside a map, perhapts it is possible to also register this bean in the context, like here.
I'm not feeling comfortable with scanning any datastructure that can contain DataSource
, like Map, Collection or anything really, one of the reasons that it might break something, the other is that it's likely a performance hit if someone is having large Map in the context that does not contain any DataSource.
Will try by dynamically registering beans.. hopefully it will work
@musabqamri123 sure, let me know how it goes. On the bright side after registering data source in the context you'll get healthchecks and metrics for free :)
Thanks it worked.