Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to Multiple databases in hibernate

 
priya aggrawal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to connect to multiple databases in hibernate using single cfg.xml file. is it possible to implement this by creating multiple session factory objects and use id mechanism to load the db configuration dynamically.

please suggest.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not from a single config file, no. But look at the methods available to you in the Configuration class - there is quite a lot of scope how you configure the SessionFactory.
 
priya aggrawal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you mean to say i need to have mutiple cfg files and multiple session factory objects. I don't want to use setter methods for defining the DB configurations. My requirement is i want to change the DB at the runtime.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need multiple SessionFactories. You may need multiple configuration files, depending on how you configure these (see my earlier post). You will need to write the DAO logic to know which SessionFactory to go to depending on the entity.

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you can let us know what is the requirement then it might be more helpful.
In case you need to connect to different databases because you are promoting your code through different development environments then that is easily achievable. While building the code for particular environment you can replace the DB using ant scripts and build.properties file.

However if the requirement is like having multiple databases connections in a single environment then you will need different SessionFactories for each of the DB as mentioned by Paul earlier.

Regds,
Amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic