使用TransactionScope的嵌套事务

Lie*_*oen 7 .net c# transactionscope

如果你有这样的话:

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);
Run Code Online (Sandbox Code Playgroud)

内部TransactionScope范围2是否也会回滚?

Ode*_*ded 15

是.

内部事务注册在外部事务的相同范围内,整个事务将回滚.情况就是这样,因为您没有使用TransactionScopeOption.RequiresNew将内部事务注册为新事务.


Kon*_*man 7

请参阅此处以获取有关此主题的说明:http://web.archive.org/web/20091012162649/http : //www.pluralsight.com/community/blogs/jimjohn/archive/2005/06/18/11451.aspx.

另请注意,这scope1.Dispose是多余的,因为scope1它将自动放置在using声明它的块的末尾.

  • 这个链接坏了. (2认同)