0

I want to migrate my app to WebFlux, but the tricky part that I have bean which connects to 6 data sources by such mechanism

public class MultiRoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return //code which sets context for chosen db;
    }
}

Then I'm creating 6 data sources which is then managed by multiRoutingDataSource

@Bean(name = "multiRoutingDataSource")
    public DataSource multiRoutingDataSource() {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(ident, MyDataSourceBean());
        MultiRoutingDataSource multiRoutingDataSource = new MultiRoutingDataSource();
        multiRoutingDataSource.setTargetDataSources(targetDataSources);
        return multiRoutingDataSource;
    }

and this data sources could be changed in runtime. This multiRouting then set into entity manager.

Is there something similar with WebFlux?

I found

public class MultiRoutingDataSource extends AbstractRoutingConnectionFactory {

    @Override
    protected Mono<Object> determineCurrentLookupKey() {
        return null;
    }

But how to create beans with connections and switch them in runtime like I'm doing in Spring MVC?

Bohdan Myslyvchuk
  • 1,657
  • 3
  • 24
  • 39

1 Answers1

3

If you want multi R2dbc connectionfactories at the same application, check my example multi-r2dbc-connectionfactories.

For multi-tenancy support, check multi-tenancy-r2dbc.

Hantsy
  • 8,006
  • 7
  • 64
  • 109
  • I took a look at multi-tenancy-r2dbc example and tried to replicate it for my application. AbstractRoutingConnectionFactory's determineTargetConnectionFactory() and hence determineCurrentLookupKey() is not getting called upon each request and as a result, multi-tenancy is not working. – aditya Aug 30 '21 at 03:18
  • @aditya Add a Github actions workflow to run the integration tests, work as expected. I am not responsible for your projects! – Hantsy Sep 01 '21 at 14:11
  • @Hantsy ,In the muti tenancy-r2dbc.. how to add a new tenent after the spring boot app loaded? For example, after the 1 master datasource and 1 tenent is initialized at the load. Now when the http request comes, based on some condition i will need to add one more tenent. Please help – Madhu Mar 03 '23 at 01:09
  • whats the difference between multi-connectionfactories and multi-tenancy approaches? – theprogrammer Apr 21 '23 at 17:37