Announcement Announcement Module
No announcement yet.
Help me with Hibernate/Spring and multiple databases Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help me with Hibernate/Spring and multiple databases

    Hi again,
    I already post similar question, but have no answer. :cry:

    I am wondering how it might be possible to work with multiple databases at the same time with Hibernate and Spring? I have a scenario with multiple 5 different databases (Oracle on different platforms). AFAIK, this would require 5 SessionFactories(each one configured for respective database), then maintaining 5 Hibernate sessions in my application.

    However, while each individual object can be guaranteed to be persisted in either one database or the other, the domain object model is split across 2 to 3 databases (part is data shared with legacy systems). This means we will be trying to load object **graphs** which span databases - The mappings dont allow objects to be mapped to Tables in different databases, and I can't readily see how we could do this directly in Hibernate. I think Spring will help, but don't know how.

    We had Some discussion about possibly front-ending the databases with third party software, and this is an option but this has performance drawbacks in our specific case.

    Has anybody acheived the same end result in some other way, or have any ideas, patterns or suggestions on how it might be possible (no matter how daft!)?

  • #2
    Hmm ... looking a lot through Hibernate source code I highly doubt that achieving something similar easily is impossible . You could possibly hack some ClassPersister classes for you mapping classes. But you probably loose all the 'beauty' that Hibernate brings you.

    >I think Spring will help, but don't know how
    I think Spring can't help you here. Spring provides nice and clean transaction handling of Hibernate's Session, DAO and template exposure, ... , but as far as object graph loading is concerned, it all depends on Hibernate API.

    I had similar request. I then wrote a small persistant library. Now I would look for different solution - maybe ' front-ending the databases with third party software'.


    • #3
      It may be possible..

      With the C-JDBC project.. Thats designed to present a single virtual database across multiple backend systems.. I have never used it myself, but some people have reported success with it.

      You can use it for "database level-raid", or for just mapping different systems together..


      • #4
        You could make your own CRUD layer you can use from your business logic layer with just Create, read, update and delete operations to the right database for every class you're mapping.

        Might not be the most interesting work to do :-).


        • #5

          There is no straight answer to you question. However I can tell you that you should search a solution inside an application server or at the db level.
          I know that oracle allows all kind of distributed scenarios so I think it would be possible to have all the databases connected by oracle and you would talk only with one session factory.
          A second alternative would be to use an application server (which has anyway the distributed transaction support). This way you can talk with 5 session factories and all the transaction will be delegated to the app server. However loading the objects from different databases is a 'fancy' problem and I don't know how exactly to handle it - Spring anyways doesn't has much to do with it as it's an IoC container and not an O/RM solution.

          Try the HB forums for advice - for sure there is some tool out there that takes care of this (at least a commercial one).


          • #6
            Found this on HIbernate forum:


            • #7
              Multiple Database with Spring and Hibernate Annotation

              Hello Friends,

              I'm new to Spring as well as Hibernate annotaion.

              Initially i had single database with two tables user and user_group.
              I had single session factory, datasouce and hibernateDaoSupport.
              And application is working fine.

              My requirement has been modified as follows

              1. Now i have two databases, one database has user data and other has user_group
              2. How do i configure two databases with spring with hibernate Annotation?
              3 What all changes to be done to achieve this?


              • #8
                Hibernate Shards was meant to solve this problem.
                look at -
                Though it show in beta right now.