Results 1 to 3 of 3

Thread: Accessing Multiple Databases with Spring and Hibernate

  1. #1
    Join Date
    Nov 2004
    Posts
    21

    Default Accessing Multiple Databases with Spring and Hibernate

    Hi,

    I like Spring and Hibernate, and think they do a wonderful job. I have a task that I haven't attempted to do with these frameworks, where I need to access database-partitioned data. Essentially I have independent databases configured with the same schema, but different user data. I can programatically figure out which database I need to access, but I need to figure out how to work with Spring and Hiberante to work well in this model.

    Essentially, a user logging in would get a session active on their destination database and the program logic would work totally consistently between different users. Presumably the hibernate caches would be unique..

    So, does anybody have any pointers to this sort of configuration?

    Thanks for your assistance!

  2. #2
    Join Date
    Sep 2004
    Location
    Toulouse, France
    Posts
    50

    Default

    I had a similar problem in a webapp (see topic), where I needed to determine which customer database to use based on the URL.

    I ended up configuring the webapp to use a different DispatcherServlet for each customer, and creating, for each servlet, a separate servletname-servlet.xml configuration file which sets a PropertyPlaceholderConfigurer to an appropriate jdbc properties file for that customer, and imports all of the other beans from a single shared config file for all servlets.

    This meant practically doing away with applicationContext.xml, since almost all beans depend on the sessionFactory which depends on the dataSource which itself depends on this servlet-specific propertyConfigurer.

    There might be a more elegant solution though!
    Best regards,
    Assaf
    Last edited by robyn; May 14th, 2006 at 12:08 PM.

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    We just use a single database with partioning via a "subscriberId" column in central tables (ie tables that are referred to by foreign keys of other tables). We have an ActiveSubscriberHolder ThreadLocal which is managed by a filter. That filter looks at the HttpSession and/or URL to set the effective subscriberId. Service layer beans (ie read*, get*, find*, create*) methods refer to the ActiveSubscriberHolder to obtain the active subscriberId and they set the appropriate properties on the bean/query before sending it to the DAO layer. We could AOP that if we wanted, but it's pretty straight forward as-is.

Similar Threads

  1. Replies: 1
    Last Post: Sep 9th, 2005, 02:22 AM
  2. Multiple Data Sources + Hibernate + Spring
    By joeserel in forum Data
    Replies: 2
    Last Post: May 18th, 2005, 09:22 AM
  3. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  4. Replies: 2
    Last Post: Mar 10th, 2005, 02:38 PM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •