Currently we use AbstractRoutingDataSource to dynamically switch data source. But sometimes switch would have some exception, then we found when switch concurrently, exception may happen. So add synchronized code when add new data source(initialize() and determineTargetDataSource()).

Comment From: pivotal-cla

@huanccwang Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: mdeinum

To prevent thread pinning (or the possibility of it) when using virtual threads you are better of using a ReenttrantLock and use proper locking in stead of syncronizing.

Comment From: jhoeller

Please note that AbstractRoutingDataSource is generally not designed for concurrent reconfiguration. It expects to be populated in a configuration step and then to act against that prepared configuration at operation time.

How do you dynamically switch the DataSource, actually? Calling setTargetDataSources and then initialize again? There is also a race condition between the setter and the init method invocation then. I'm afraid that simple synchronization/locking in the existing methods won't be sufficient, this rather requires some specific design for dynamic reconfigurability.

From that perspective, please rather state your requirements as an enhancement request in a separate GitHub issue. We may then discuss an explicit arrangement for dynamic reconfiguration there.