使用TransactionScope的嵌套事务

时间:2009-12-04 08:01:56

标签: c# .net 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);

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

2 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:7)

请点击此处查看有关此主题的说明:http://web.archive.org/web/20091012162649/http://www.pluralsight.com/community/blogs/jimjohn/archive/2005/06/18/11451.aspx

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