Configure Hibernate to use a database whose name is not known until runtime

I have a web application running on Java 6, Spring 2.5.6 and Hibernate 3.2.7. Now you need to get some data from several databases whose names are unknown before execution. What is the best way to achieve this?

I looked, for example. in the article http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/ , but this, apparently, is applicable only in a situation where all database configurations are known in advance.

+5
source share
3 answers

You can create a class that implements org.hibernate.connection.ConnectionProvider. Then, in your hibernate configuration file, add this class as follows:

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

You probably need a different sessionFactory for each database. Can you provide more details on how your application learns about database connections at runtime?

+2
source

All Articles