Forums Register Login

how to connect to multiple databases dynamically using Hibernate

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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;
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Again, ignoring the presence of Hibernate, how would you do this in JDBC? How does your controller communicate which data source it is using?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi,

Feel free to ask the code for this.
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 52189 times.
Similar Threads
Configure Database dynamically in Hibernate
How to connect multiple databases at a time using Hibernate
how can i configure spring with hibernate shard?
Connecting to Multiple databases in hibernate
Connecting to multiple DataBase
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:29:55.