SisoDb now supports TransactionScope

When using an UnitOfWork in SisoDb you are using traditional ADO.Net transactions. Lets have a look at an example:

using(var unitOfWork = dataBase.CreateUnitOfWork())
{
    unitOfWork.InsertMany(customers);
    unitOfWork.Commit();
}

This is all fine but suppose you want to control several UnitOfWorks, well the obvious solution is to use the TransactionScope class. Since we are targetting SQL Server 2008 we also don’t want to escalate to an distributed transaction, even if multiple connections are opened targeting the same DB using the same connection string. This is now supported. Hence you can do things like:

using(var ts = new TransactionScope())
{
    using(var unitOfWork = dataBase.CreateUnitOfWork())
    {
        unitOfWork.InsertMany(customers);
        unitOfWork.Commit();
    }

    ts.Complete();
}

The UnitOfWork will check if there is an ongoing Transaction from a TransactionScope, and if there is, no ADO.Net transactions will be created and the Commit and Rollback of the UnitOfWork is left to the outer TransactionScope.

You can of course have multiple UnitOfWorks as well:

using(var ts = new TransactionScope())
{
    using(var unitOfWork = dataBase.CreateUnitOfWork())
    {
        unitOfWork.InsertMany(customers1);
        unitOfWork.Commit();
    }

    using(var unitOfWork = dataBase.CreateUnitOfWork())
    {
        unitOfWork.InsertMany(customers2);
        unitOfWork.Commit();
    }

    ts.Complete();
}

Hope it helps you.

//Daniel

One thought on “SisoDb now supports TransactionScope

  1. Pingback: TransactionScope « Blog for SisoDb

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s