R – Handling Multiple databases with NHibernate in a single application

multiple-databasesnhibernatesession

At the moment I define the connection properties in a configuration file and only ever connect to one database. I'd like to be able to at some point have a user login, figure out (via a seperate central database maybe) what database they should be connected and from that point on all sessions created will talk to that database.

Whats the best way to achieve this. Create a configuration file for every possible database? Or could I have a single session manager and change the connection url on the fly accordingly? What sort of options do i have?

Update: Apologies I should have mentioned this was NHibernate. I didn't think it would matter but some things like Hibernate Shards will not be applicable to be as I believe NHibernate Shards is waiting.

Best Answer

You just need to make two datasources then call the one you need for the specific query.

Please take a look at this:

https://www.hibernate.org/450.html

Some official solutions.

And here:

http://www.java-forums.org/database/159-hibernate-multiple-database.html

an online thread about this issue.

Related Topic