简体   繁体   中英

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 eg 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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM