Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Search Using AbstractRoutingDataSource

 
Greenhorn
Posts: 4
IBM DB2 Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
harsha edupuganti
Greenhorn
Posts: 4
IBM DB2 Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Get off me! Here, read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic