Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Transactions with TransactionScope

If you have somehting like this:

IBinaryAssetStructureRepository rep = new BinaryAssetStructureRepository();
var userDto = new UserDto { id = 3345 };
var dto = new BinaryAssetBranchNodeDto("name", userDto, userDto);
using (var scope1 = new TransactionScope())
{
    using(var scope2 = new TransactionScope())
    {
        //Persist to database
        rep.CreateRoot(dto, 1, false);
        scope2.Complete();
    }
    scope1.Dispose();
}
dto = rep.GetByKey(dto.id, -1, false);

Will the inner TransactionScope scope2 also be rolled back?

like image 939
Lieven Cardoen Avatar asked Dec 04 '09 08:12

Lieven Cardoen


2 Answers

Yes.

The inner transaction is enrolled in the same scope of the outer one, and the whole thing will rollback. This is the case, as you didn't enroll the inner transaction as a new one using TransactionScopeOption.RequiresNew.

like image 114
Oded Avatar answered Nov 10 '22 10:11

Oded


See here for an explanation on this subject: http://web.archive.org/web/20091012162649/http://www.pluralsight.com/community/blogs/jimjohn/archive/2005/06/18/11451.aspx.

Also, note that the scope1.Dispose is redundant since scope1 will be automatically disposed at the end of the using block that declares it.

like image 24
Konamiman Avatar answered Nov 10 '22 08:11

Konamiman