| Author |
Search Using AbstractRoutingDataSource
|
harsha edupuganti
Greenhorn
Joined: Jun 29, 2012
Posts: 4
|
|
The basic idea behind AbstractRoutingDataSource is that a routing DataSource acts as an intermediary, while the 'real' DataSource can be determined dynamically at runtime based upon a lookup key.
If I need to implement a search functionality and I dont know which DataSource to use prior to compilation, how do I assign the look-up key for a particular schema at run-time?
I am running the example posted here. http://blog.springsource.org/2007/01/23/dynamic-datasource-routing/
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?
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
and I have no idea about the schema of the Item
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.
|
[How To Ask Questions][Read before you PM me]
|
 |
harsha edupuganti
Greenhorn
Joined: Jun 29, 2012
Posts: 4
|
|
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.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
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.
|
 |
 |
|
|
subject: Search Using AbstractRoutingDataSource
|
|
|