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?
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?
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.
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
saranga, you are correct this is one way of using more than one DataSourceif 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 ]
Joined: Apr 24, 2007
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.
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.