[英]Question About TransactionScope in .NET
using (TransactionScope scope = new TransactionScope())
{
int updatedRows1 = custPh.Update(cust.CustomerID, tempPh1, 0);
int updatedRows2 = custPh.Update(cust.CustomerID, tempPh2, 1);
int updatedRows3 = cust.Update();
if (updatedRows1 > 0 && updatedRows2 > 0 && updatedRows3 > 0)
{
scope.Complete();
}
}
上面的TransactionScope代碼是否正確構造? 這是我第一次使用它,所以我想盡可能簡單。
鎖好了,
但你正在做的是糟糕的設計。 如果不是每個表都有更新的行,您基本上都在進行回滾。 您永遠不會知道您的交易是否已完成或失敗。 這可能使解決錯誤成為一種痛苦。
如果出現問題,我寧願拋出異常。 這也會導致回滾。 因為從未達到scope.Complete()。
using (TransactionScope scope = new TransactionScope())
{
int updatedRows1 = custPh.Update(cust.CustomerID, tempPh1, 0);
int updatedRows2 = custPh.Update(cust.CustomerID, tempPh2, 1);
int updatedRows3 = cust.Update();
if (updatedRows1 == 0 || updatedRows2 == 0 || updatedRows3 == 0)
throw new Exception("Not all rows could be updated");
scope.Complete();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.