How to programmatically change databases in Spring with one DataSource?
You can do this by extending the Spring's AbstractRoutingDataSource
and wrapping your existing data sources in it. Check this article for details. Quoting from the article:
The general idea is that a routing DataSource acts as an intermediary – while the 'real' DataSource can be determined dynamically at runtime based upon a lookup key.
Also see similar questions on SO:
- Using AbstractRoutingDataSource to dynamically change the database schema/catalog
- Reading from multiple Db's with same Persistence Unit?
- How to create Dynamic connections (datasource) in spring using JDBC