Situation is that you want to add DataSource to the DataSources of AbstractRoutingDataSource during runtime.

When i put a Data Source, I need to synchronize target Data Sources to resolved Data Sources.

At that time, I call afterPropertiesSet() as below.

@Component
public class MultiDatabaseManager {

    private final Map<Object, Object> dataSourceMap = new ConcurrentHashMap<>();

    private AbstractRoutingDataSource multiDataSource;

    public DataSource createMultiDataSource() {
        MultiDataSource multiDataSource = new MultiDataSource();
        multiDataSource.setTargetDataSources(dataSourceMap);
        multiDataSource.setDefaultTargetDataSource(defaultDataSource());
        return multiDataSource;
    }

    public void addDataSource(String hostIp, ...) throws SQLException {
        DataSource dataSource = DataSourceBuilder
                .create()
                .username(username)
                .password(password)
                .url(url)
                .driverClassName(driverClassName)
                .build();

        try (Connection c = dataSource.getConnection()) {
            dataSourceMap.put(hostIp, dataSource);
            multiDataSource.afterPropertiesSet(); // unclear intentions
        }
    }

}

However, afterPropertiesSet() is unclear.

So I suggest extract refresh() method. Then, intent can be exposed.

Please feel free to share your opinions or advice.

thanks you.

Comment From: violetbeach

Can you please also introduce a similar refresh() method in AbstractRoutingConnectionFactory along with a corresponding test?

Sure. I've done.

Please let me know if you need anything else. Thank you for your review!

Comment From: violetbeach

After further consideration, the team has decided that we would like these new methods to be named initialize() instead of refresh(), since the latter implies that there is first class support for dynamic refreshes at runtime -- which is not the case.

I get it.

Thank you for considering it and the kind comment!

Comment From: sbrannen

This has been merged into main in 6d2d8a36c2b608e0c127efa20ef9ea8f8238ff84 and revised in d50ec68ad7bbbc195353e1ba59e2a613bc80013a.

Thanks for the contribution, and congratulations on getting your first PR for the Spring Framework merged! 👍