Bohdan Myslyvchuk
Bohdan Myslyvchuk

Reputation: 1817

Multiple R2DBC datasource with Spring boot

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?

Upvotes: 0

Views: 3086

Answers (1)

Hantsy
Hantsy

Reputation: 9281

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

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

Upvotes: 3

Related Questions