C# 当根事务回滚时,嵌套事务是否会回滚?
在此代码中C# 当根事务回滚时,嵌套事务是否会回滚?,c#,sql-server,transactions,transactionscope,rollback,C#,Sql Server,Transactions,Transactionscope,Rollback,在此代码中 public static TransactionScope CreateTransactionScope(bool createNew = false) { return new TransactionScope( createNew ? TransactionScopeOption.RequiresNew : TransactionScopeOption.Required, new TransactionOptions() { Isolat
public static TransactionScope CreateTransactionScope(bool createNew = false)
{
return new TransactionScope(
createNew ? TransactionScopeOption.RequiresNew : TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted });
}
事实上,在这一次
using (TransactionScope rootScope = CreateTransactionScope())
{
using (TransactionScope nestedOne = CreateTransactionScope())
{ nestedOne.Complete(); }
using (TransactionScope nestedTwo = CreateTransactionScope(true))
{ nestedTwo.Complete(); }
// No committing, rollback 'rootScope'.
}
哪些事务将随根范围一起回滚-它将仅是
nestedOne
还是同时是nestedOne
和nestedTwo
?nestedOne将加入根范围,因此如果根范围将回滚,nestedOne也将回滚,但不是nestedTwo,这是一个单独的事务
就像您有“RequireRenew”选项将事务与封闭事务分开一样,您也可以有“Suppress”选项停止该范围内的事务
请看MSDN中的以下列表,它提供了有关事务行为的重要经验。