Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Connecting two different databases using Hibernate

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am developing a project using Hibernate and the requirement is to connect to two different Databases
1. ORacle and
2. SQL Server.
The same set of tables exist in both the tables.(i.e. name of Tables and their fields are same in both the databases).
Both the databases need to be accessed during the life cycle of request from user.
How I can I achieve this using Hibernate ?
I have read few blogs on web, some speaks:
- by making two different hibernate.cfg.xml file and having two SessionFactory Instances.
- Somewhere I can see as Using JTA.
- Some says using ThreadLocal .


But I don't get any stepwise example of codes for any of above approaches.
Can someone plesae help me to point to such step by step solution from where I can get this solved with better understanding of these approaches.

The more descriptive will be more appreciated as I am not expert in Hibernate

regards,
Dhaansumaal
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

- by making two different hibernate.cfg.xml file and having two SessionFactory Instances.



This is exactly what you do.

You need two hibernate configuration files. You'll need to call them hibernate_sql.cfg.xml and hiberante_oracle.cfg.xml or something like that to differentiate them.

Then, when you initialized your Configuration object, you specifiy the name of the config files. So, you'll have two Configuration objects, from which you'll get two SessionFactory objects.

When you need an Oracle session, go to the SessionFactory created with the oracle configuration file. Do the same thing for SQL when an SQL file is needed.

There is also the Hibernate Shards project for connecting to multiple databases, but that might be more tooling than you need.

-Cameron McKenzie
 
marlajee Borstone
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Cameron, thanks for directing me to one specific approach.
However, since the name of Tables and their structure are same in both the databases (SQL & Oracle), does a Single POJO class will meet the pupose of two different databases here ?
Will it not affect the caching and performation of application ?


regards,
Dhansuumaal
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you're blazing new trails on this one. I'm not experienced with this particular scenario, so don't take my word as gospel.

I do believe the classes must be different, so the may have the same names, but they would need to be placed in separate packages. It's certainly sounding a little messy.

If you get it to work, I certainly think it would make an interesting blog, so don't keep any hibernate magic you 'conjure up' to yourself.

-Cameron McKenzie

 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic