This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In this example, the "routing" DataSource is supplied to the Catalog so that it can dynamically get connections from the 3 different databases at runtime based on the current customer's type. The CustomerType is set manually and the list of Items in that Schema is extracted.
Suppose, I want to perform a search operation based on an item's price (and I have no idea about the schema of the Item), how do I implement that? how do I determine the schema in which the Item is present using AbstractRoutingDataSource?
A solution like this is to reuse your repositories and queries. The database schema for your item must be equivalent across all the datasources or your queries will fail to work against all of them. If that is not what you meant by schema of the item please clarify.
Thats right. The database schemas across all the datasources are equivalent.
What I meant was that I dont know where the item is present. The schema in which the item is present is to be determined at runtime.
OK. So what are the rules to figure out what data source you need to query? Going by the example shown on the spring source blog you referenced, in your thread local context holder you would store any information about the item that you need to ascertain which datasource to use then use that information in the class extending AbstractRoutingDatasource to return a key in the Map of datasources.
Nothing has to be static, at run time when you have the information you need set it in your context holder so it is available when the query is made.