关于 .NET 中 TransactionScope 的问题
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 代码的结构是否正确?这是我第一次使用它,所以我试图尽可能简单。
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();
}
}
Is the above TransactionScope code structured correctly? This is my first time using it so i'm trying to make as simple as i can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
锁得很好,
但你所做的设计很糟糕。
如果不是每个表都有更新的行,那么您基本上正在执行回滚。
您永远不会知道您的交易是否完成或失败。这可能会让解决错误变得很痛苦。
如果出现问题,我更愿意抛出异常。这也会导致回滚。因为永远不会达到scope.Complete()。
Locks fine,
but what you are doing is bad desing.
You are basically doing a rollback if not every table has updated rows.
You will never know if your transaction completed or failed. Which could make resolving an error a pain.
I would prefer throwing the exception if something went wrong. That would lead to a rollback, too. because scope.Complete() is never reached.