#StackBounty: #spring #spring-boot #jpa Include newly added data sources in to route Data Source object with out restarting the applica…

Bounty: 50

Implemented Spring’s AbstractRoutingDatasource by dynamically determining the actual DataSource based on the current context.
Refered this article : https://www.baeldung.com/spring-abstract-routing-data-source.

Here on spring boot application start up . Created a map of contexts to datasource objects to configure our AbstractRoutingDataSource. All these client context details are fetched from a database table.

@Bean
    @DependsOn("dataSource")
    @Primary
    public DataSource routeDataSource() {

        RoutingDataSource routeDataSource = new RoutingDataSource();

        DataSource defaultDataSource = (DataSource) applicationContext.getBean("dataSource");

        List<EstCredentials> credentials = LocalDataSourcesDetailsLoader.getAllCredentails(defaultDataSource); // fetching from database table

        localDataSourceRegistrationBean.registerDataSourceBeans(estCredentials);

        routeDataSource.setDefaultTargetDataSource(defaultDataSource);
        Map<Object, Object> targetDataSources = new HashMap<>();

        for (Credentials credential : credentials) {
            targetDataSources.put(credential.getEstCode().toString(),
                    (DataSource) applicationContext.getBean(credential.getEstCode().toString()));
        }
        routeDataSource.setTargetDataSources(targetDataSources);

        return routeDataSource;
    }

The problem is if i add a new client details, I cannot get that in routeDataSource. Obvious reason is that these values are set on start up.

How can I achieve to add new client context and I had to re intialize the routeDataSource object.

Planning to write a service to get all the client context newly added and reset the routeDataSource object, no need to restart the server each time any changes in the client details.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.