5

I have a web application running on Java 6, Spring 2.5.6 and Hibernate 3.2.7. Now there is a requirement to fetch some data from several databases, whose names are not known before the runtime. What is the best way to achieve this?

I have looked e.g. into the article at http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/, but that seems to be applicable only in a situation, where all the database configurations are known beforehand.

3 Answers 3

2

You can do configuration in Java code, so you can do it at runtime:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-programmatic

Sign up to request clarification or add additional context in comments.

2 Comments

It seems like I can set a datasource for the Configuration object, but this still requires that all possible data sources are defined before the application starts (or maybe not, which gets back to the original question).
You can create Configuration whenever and how many you like. But you should also be able to manage to chose one of them when you need to use them.
2

You could create a class that implements org.hibernate.connection.ConnectionProvider. Then in your hibernate configuration file add that class like this:

<property name="hibernate.connection.provider_class">my.class.that.implements.ConnectionProvider</property>

You probably want a different sessionFactory for each database too. Can you provide any more information about how your app finds out about the database connections at runtime?

3 Comments

It's about connecting to a Quality Center site admin database. Each Quality Center project has its own database, whose names I read from a table. The schema for each database is known, only the database name is not known until getting the information from the QC stie admin database.
You will want a unique sessionFactory for each connection. You could do this at application startup or as needed. You can use a combination of programtic configuration and config based configuration to do this. For example create a config file that has everything required except for the db connection. Then do this: configuration.configure("hibernate.cfg.xml").setProperty("dbstuff","dbvalue")
@simon Another alternative would be to create the connections yourself. Then using a single session factory: sessionFactory.openSession(connection); Javadocs recommend using a connectionProvider instead as the second level cache will be disabled using this method.
2

As the database structure is same for each database (although their names are not known beforehand), I decided to impement it simply by adding the database name to the query as parameter. This avoids the resource and management problems from using multiple session factories.

More information from here: http://web.archive.org/web/20071011173719/http://hibernate.org/429.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.