views:

973

answers:

3

As above really.

A: 

Well, it may be tricky but and I think it should not be done, but here's the idea:

var cfg = new Configuration(); // Get a new NHibernate Configuration
cfg.SetProperty("connection.connection_string", yourConnectionString); // Alter the property
cfg.Configure(); // Configure with this configuration
var sf = cfg.BuildSessionFactory(); // Get a new ISessionFactory

I don't know if this is the best method, but may work. You could as well have 2 configuration xml files and do this:

var cfg = new Configuration("hibernate1.cfg.xml"); // OR
var cfg = new Configuration("hibernate2.cfg.xml");

Responding to the comments, you can have several session factories initialized and ready for use, but that's not exactly "change the connection string". Then choose the one you need. The costly part is creating the Configuration object and calling Configure().

There's as well NHibernate Burrow library, that manages several sessions at the same time and chooses the right one depending on the entity.

var session = new BurrowFramework().GetSession(); // default session
var session = new BurrowFramework().GetSession(typeof(Entity)); // session that manages Entity class
Marc Climent
Thanks Marc, however I thought that the SessionFactory should only be created once though because it is an expensive procedure?
John_
I changed my response for completeness. As @joshua.ewer said, Burrow can help. Anyway, avoid creating session factories more than once per configuration.
Marc Climent
A: 

The SessionFactory is what sets up your connections, dialects, etc, so if you want to change your connection string, you'll need to reconfigure. But, yeah, your comment is right. Setting up a session is one of the most expensive operations, so you only want to do it when you need to and never any more.

There are some really great conversation concepts in nHibernate Burrow. If you take a look at how they are managing session, you might find some creative ideas to manage multiple sessions across multiple database.

I have a suggestion that the same as @Marc's principle: you can nicely encapsulate dynamic connections in a provider. Take a look at the nHForge wiki regarding dynamic connection providers. You can easily build a provider in which you pass in the information you need in order to build up a session pointing to the database you desire.

I'm using nHibernate 2.0 on top of 100+ databases (all have same schema, but physically separated data) so it can definitely be done with a little TLC ;-)

joshua.ewer
Setting up an ISession is actually extremely cheap. What *is* expensive is the creation of ISessionFactory.
Anton Gogolev
I was replying to @marc's comment/question of "SessionFactory should only be created once", but after looking at my answer, my wording does imply that Session is the expensive one. ;-(
joshua.ewer
+4  A: 

An ISessionFactory.OpenSession() can be supplied with a IDbConnection.

Another option is to implement a IConnectionProvider wich would create appropriate IDbConnection instances depending upon some condition (which has to be global, thus rendering this solution not very clean).

Anton Gogolev
Exactly what I wanted, simple and does what it says on the tin.
John_
Note that the connection needs to be opened before using it. NHibernate won't do it for you (I just learned).
nickd