Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Hibernate to use database, whose name is not known before runtime

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.

like image 856
simon Avatar asked Feb 17 '11 10:02

simon


People also ask

How does Hibernate connect to database?

It represents the classname of a custom ConnectionProvider which provides JDBC connections to Hibernate. It is used to set the JDBC transaction isolation level. It enables auto-commit for JDBC pooled connections.

Does Hibernate uses JDBC internally?

Hibernate and JPA-based applications do not eliminate the need for JDBC. When you use Hibernate, all database interactions still occur with the JDBC APIs. Hibernate and JPA are actually built on top of the JDBC API.

What is configuration in Hibernate?

Configuration represents an entire set of mappings of an application's Java types to an SQL database. The org. hibernate. cfg. Configuration is used to build an immutable org.


1 Answers

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

like image 123
Ali Shakiba Avatar answered Sep 30 '22 09:09

Ali Shakiba