views:

68

answers:

1

We are having a few problems understanding how best to use NHibernate. We typically have a relatively large number of quite small (in terms of number of tables) SQL Server databases rather than one database with a lot of objects.

We are looking at various options for handling multiple session factories and probably have this under control. However we are not sure how we would wrap all calls in a single transaction. Using hand-rolled data access you would just wrap it all in a TransactionScope but we are a little reluctant to do this with NHibernate as it seems to like to handle all its own Transactions.

Using multiple databases with shared transactions seems like the sort of thing lots of people would want to do with NHibernate.

Are we just barking up the wrong tree entirely?

+1  A: 

You can safely use TransactionScope. But you have to open NHibernate transactions too.

Example:

using (var ts = new TransactionScope())
using (var session1 = sessionFactory1.OpenSession())
using (var tx1 = session1.BeginTransaction())
using (var session2 = sessionFactory2.OpenSession())
using (var tx2 = session2.BeginTransaction())
{
    //Do work...
    tx1.Commit();
    tx2.Commit();
    ts.Complete();
}
Diego Mijelshon
From where do we reference TransactionScope?
TransactionScope is in System.Transactions. You can instaniate one anywhere.Incidentally I'm just waiting for the time to test this answer before I accept it!
Gaz