Results 1 to 3 of 3

Thread: Multiple Database Access with Spring & Hibernate

  1. #1
    Join Date
    Oct 2004
    Location
    Dallas, TX
    Posts
    20

    Default Multiple Database Access with Spring & Hibernate

    We have a situation on the project I'm working on in which we need to use multiple schemas to contain the same domain model information. I've come up with the following idea for a solution and wanted to run it past the forums just in case there is something I've missed.

    We'll be using a single Hibernate SessionFactory. I'll create a DataSource implementation that takes a value from a ThreadLocal (indicating which database the user wishes to use) set and cleaned up afterwards within a Servlet Filter from a HTTP Session attribute. I will then delegate to the data source for that database. Basically the SessionFactory will think it is dealing with a single data source but would be getting connections to different databases from different DataSources.

    It would seem to me that everything "above" the SessionFactory (Spring helper classes, my DAOs, etc. etc.) wouldn't be aware of or affected by the changing connections. Anyone see a big, huge, gaping hole in this idea?

    Thanks,
    C

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    The Spring classes as well as the DAOs should work great. I do, however, see two issues here:
    1. The management of the Hibernate second level cache. If it happen that you have some classes that are mapped to tables in both schemas, you need to clear the second level cache on each request. This is bad for scalability.
    2. Distributed transactions: I do not know if you need this in your case.

    If you will be using Oracle databases, you may consider using DB-Links.
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Oct 2004
    Location
    Dallas, TX
    Posts
    20

    Default

    Quote Originally Posted by irbouho
    The Spring classes as well as the DAOs should work great. I do, however, see two issues here:
    1. The management of the Hibernate second level cache. If it happen that you have some classes that are mapped to tables in both schemas, you need to clear the second level cache on each request. This is bad for scalability.
    2. Distributed transactions: I do not know if you need this in your case.

    If you will be using Oracle databases, you may consider using DB-Links.
    HTH
    Thanks for the feedback! I agree that Hibernate's second-level cache won't be able to be used at all in this case. But, since we have a mostly read-write domain model I'm not too sure we'd gain too much from it anyway.

Similar Threads

  1. Replies: 1
    Last Post: Oct 2nd, 2005, 10:16 AM
  2. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  3. Replies: 2
    Last Post: Nov 12th, 2004, 05:23 AM
  4. Replies: 1
    Last Post: Oct 20th, 2004, 05:40 AM
  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
  •