File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes how to connect to multiple databases dynamically using Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to connect to multiple databases dynamically using Hibernate" Watch "how to connect to multiple databases dynamically using Hibernate" New topic
Author

how to connect to multiple databases dynamically using Hibernate

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

I have a requirement in which I should be able to connect to multiple databases dynamically using Hibernate.
I can use multiple configuration files and call configure(String filename) to create SessionFactory object. But my requirement is that multiple databases connect to my server dynamically which I am unaware of, so I would not be able to configure them in prior in separate config files.

As I am fresh to Hibernate, could anyone help me in resolving this issue?

Thanks,
Deepa
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ignore Hibernate for a moment, how would you do this from a JDBC point of view? At some point your application is going to have to establish a connection to a database and it is goingto have to know which database it is trying to connect to.


But my requirement is that multiple databases connect to my server dynamically which I am unaware of,

The database connects to the server? What do you mean by this?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
hii


this is how we can do

<?xml version="I.0" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
<hibernate-configuration>
<session-factory>
<property name="connection.driver class">
com.microsoft.jdbc.Driver</property> <property name="connection.url">
jdbc.Microsoft://localhost/products</property> <property
name="username">sa</property>
<property name="password">sa</property>
</session-factory>
</hibernate-configuration>


code is like this

this for mysql database


Configuration configuration = new ConfigurationO.addClass(classname);
SessionFactory mysqlSession = configuration.buildSessionFactoryQ;


for sql database server


configuration = new Configuration().
configure("sglserver.cfg.xml").addClass(Class Name);
SessionFactory sqlserverSession = configuration.buildSessionFactoryO;
Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60
Hibernate Shards is the way to go...

Its a sub project of Hibernate, through which you can access multiple datasources.

Have a look at this link..

Hibernate Shards Doc

Regards
Atul
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

Thanks for the response.

I would like to explain the architecture we have-
There is a GUI, a controller and a model. This model could be a distributed one, ie., there can be no. of datasources running in multiple servers.

For the connectivity between the controller and the model, we are using Hibernate. And these datasources are connected dynamically to the controller at run-time. Hence we do not know the connectivity parameters beforehand. So, how to resolve this?

The suggestions given below are possible to implement only if we know the connectivity parameters beforehand, but this is not the case here.
Please let me know if this is feasible.

Thanks,
Deepa
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Again, ignoring the presence of Hibernate, how would you do this in JDBC? How does your controller communicate which data source it is using?
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
HI

Configure the database in server.xml (if you are using tomcat or else in admin-console) and Use the jndi-naming look up in hibernate or in java code


Regards
saranga
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
Hi,

Feel free to ask the code for this.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by saranga rao:
HI

Configure the database in server.xml (if you are using tomcat or else in admin-console) and Use the jndi-naming look up in hibernate or in java code


Regards
saranga


saranga, you are correct this is one way of using more than one DataSource if this is a web application deepa gr is talking about and if they are using Tomcat. And of course typically in a JEE applciation JNDI would be the repository used to lookup the data source(s) since it is accessable in a distributed environment. However the requirement that is the real spanner in the works is this:

But my requirement is that multiple databases connect to my server dynamically which I am unaware of, so I would not be able to configure them in prior in separate config files.

Its the "which I am unaware of" part that is confusing me, but it sounds like they need to configure DataSources dynamically at runtime. Am I a right is this supposition deepa gr?

[ May 08, 2007: Message edited by: Paul Sturrock ]
[ May 08, 2007: Message edited by: Paul Sturrock ]
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
HI,

I assume that with out knowing the which database they are using in the project they cannot do.this is one of the approach to accessing the multiple database.


Regards
Saranga
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41182
    
  45
It's no problem to create several Configuration objects at runtime that point to different databases. Furthermore, it's not necessary to have the properties that describe those DBs in a file or hardcoded anywhere - they can be obtained dynamically in any number of ways. Which brings us back to Pauls question, and -barring an answer by deepa- I think we won't get anywhere.


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: how to connect to multiple databases dynamically using Hibernate
 
Similar Threads
How to connect multiple databases at a time using Hibernate
Configure Database dynamically in Hibernate
Connecting to multiple DataBase
how can i configure spring with hibernate shard?
Connecting to Multiple databases in hibernate