TransactionScope error in ambient transaction does not rollback the transaction

前端 未结 3 477
臣服心动
臣服心动 2021-01-25 02:35

I use an ambient transaction like this :


using(TransactionScope tran = new TransactionScope()) {
    CallAMethod1();//INSERT
    CallAMethod2();//INSERT
           


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-25 03:12

    Read what Khanh TO says. If your connection is opened outside the outer transaction scope the connection won't be enlisted.

    That is why the first call didn't rollback when the second failed. You will have to enlist your connection:

    using (TransactionScope tran = new TransactionScope(TransactionScopeOption.Required))
    {
       connection.EnlistTransaction(Transaction.Current);
       CallAMethod1();//INSERT
       CallAMethod2();//INSERT
       tran.Complete();
    }
    

提交回复
热议问题