2

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
  • You might want to look into the source of the NHibernate Sharding project to get some ideas because it sounds like you have a sharded database. Commented Jun 11, 2010 at 16:05

1 Answer 1

3

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

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!
@Pingpong no idea, depends on whether MySql supports transaction scopes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.