The DAO pattern makes it very easy to create an application that can use multiple databases, with minimal changes to support that. So, if you're using DAOs, it shouldn't be too difficult a switch.
you can get something to spring and hibernate to connect to multiple datasource
Justin Chi
Greenhorn
Joined: Sep 09, 2009
Posts: 25
posted
0
v chenna wrote:I am developing Hibernate application which it has to support to connect multiple databases with out changing the code.
For example, client has his own choice to select a data base(Oracle,SQL Server,My SQL...) code has to support.
please provide the code or any link or clue for this
bye.
This can not be done only by configuration.
You will need multiple hibernate DB configuration files per database like hibernate.oracle.cfg.xml, hiberante.mysql.cfg.xml , hiberante.sqls.cfg.xml , etc.
Then there better be a property file (db.properties) telling which db is being used by application.
Created a class DBBundle ( extends java.util.ResourceBundle) mapping to db.properties, suppose your configuration in db.properties is myDB=oracle.
Then can map to db as below :
if ("oracle".equals(DBBundle.myDB){
new Configuration().configure("hibernate.oracle.cfg.xml").buildSessionFactory();
}else if ("mysql".equals(DBBundle.myDB){
new Configuration().configure("hibernate.mysql.cfg.xml").buildSessionFactory();
}else if (.....){
.....
}else{
// throw exception ......
}
And I am sure you can make it more configurable.
This message was edited 4 times. Last update was at by Justin Chi
v chenna
Greenhorn
Joined: Dec 28, 2009
Posts: 11
posted
0
Thanks Justin for providing valuable information. If you don't mine can i get complete code or related material.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
If you don't mine can i get complete code or related material.
This is all standard DAO pattern and Hibernate stuff. It would appear that the problem is not the lack of code examples being provided to you. From the links provided, I think you have been given more than enough information to start building your own program.
Try out some code, and see what works and what doesn't. Ask us question about problems you're having, and we'll help.
From the difficulty you're having understanding the code samples already provided, I'd say that you're probably having some trouble with basic Hibernate and DAO concepts. Learn the fundamentals of Hibernate, and work through that DAO tutorial that I provided - all of the code is there. Please give it a try, do some coding of your own, and tell us where you're having problems. We can't actually code a solution to your problem for you.
-Cameron McKenzie
subject: How to connect multiple databases at a time using Hibernate