Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to connect to multiple databases dynamically using Hibernate

 
ram shyam
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
saranga rao
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Feel free to ask the code for this.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic