Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Nhibernate session.BeginTransaction auto rollback on exception within Using

Ok sorry for the long subject name...

If I do the following:

using (var transaction = session.BeginTransaction())
{
    // do something
    transaction.Commit();
}

If my do something caused an exception, would it auto rollback, or do I need to explicitly check for this like below:

using (var transaction = session.BeginTransaction())
{
    try
    {
        // do something
        transaction.Commit();
    }
    catch (Exception)
    {
        transaction.Rollback();
    }
}
like image 719
somemvcperson Avatar asked Jun 16 '11 19:06

somemvcperson


1 Answers

It's a safe assumption that the transaction will be rolled back if the commit fails in a using block.

ITransaction wraps an ADO.NET transaction behind the scenes, so it depends on the provider specific implementation. The Dispose method in the source code I checked (2.1) assumes that calling Dispose on the internal IDbTransaction rolls it back.

like image 140
Jamie Ide Avatar answered Sep 28 '22 08:09

Jamie Ide