Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Transaction scopes

danielwertheim edited this page Feb 19, 2012 · 11 revisions

Under the hood, each Session makes use of plain ADO.Net transactions. But if the provider supports it and there's an ongoing Transaction from e.g a transaction-scope, an ADO.Net transaction will not be started hence the Transaction-scope will take control of the outcome. So normally, this:

using(var session = db.BeginWriteSession())
{
    session.InsertMany(customers);
}

will begin a ReadCommitted ADO.Net transaction associated with the connection. But if you have wrapped it in a transaction-scope, Transaction.Current will have a value and then the Session will not explicitly create an own transaction; hence it will implicitly be controlled by the outer transaction-scope.

using (var t = new TransactionScope(TransactionScopeOption.Required))
{
    using (var session = db.BeginSession())
    {
        session.InsertMany(new[]
        {
            new IdentityItem {Value = 1},
            new IdentityItem {Value = 2},
            new IdentityItem {Value = 3}
        });
    }

    using (var session = db.BeginSession())
    {
        session.InsertMany(new[]
        {
            new IdentityItem {Value = 4},
            new IdentityItem {Value = 5},
            new IdentityItem {Value = 6}
        });
    }

    //t.Complete - To commit changes
}
Clone this wiki locally