Alex Rivera | Logout

NHibernate with TransactionScope

Asked 2009-03-14T17:33:07.857
34

Can anyone give me a quick overview of using TransactionScope with NHibernate? Do I need to do anything special with the session/IEnlistmentNotification/etc. to get this to work? Are there any pitfalls that I should worry about? For example, can I replace all of my hibernate transactions:

var transaction = session.BeginTransaction();
try
{
    // code
    transaction.Commit();
}
catch (Exception)
{
    transaction.Rollback();
}

with this?:

using (var scope = new TransactionScope())
{
    // code
    scope.Complete();
}
Edit
Report

1 Answer

6

The above works OK provided you are using a connection provider that supports the use of a Light-weight Transaction Manager, such as SQL Server 2005/2008.

If you are using SQL Server 7/2000 then all of your transactions will become Distributed Transactions even if you only hit one database/resource. This is probably not what you would want in most cases, and will be expensive performance wise.

So checkout if your connection provider and database server combination are suitable for use with TransactionScope.

answered 2009-07-06T09:55:07.310

Your Answer