如何重试因死锁而失败的事务?
我使用 linq-sql 这就是我所需要的。
Sub Commit()
Try
StepStart:
Transaction Scope Serialized
Begin Transaction
Check BusinessRule1
Check BusinesRule2
if BusinessRulesFailed
{ Transaction.Rollback }
else {
Query = db.out
db.SubmitChanges()
Transaction.Commit()
}
Catch DeadLockException
Goto StepStart
End Try
End Sub
遵循这种方法可以吗?有人可以给我一个 C# 或 VB.Net 的例子吗
I use linq-sql and this is what I need.
Sub Commit()
Try
StepStart:
Transaction Scope Serialized
Begin Transaction
Check BusinessRule1
Check BusinesRule2
if BusinessRulesFailed
{ Transaction.Rollback }
else {
Query = db.out
db.SubmitChanges()
Transaction.Commit()
}
Catch DeadLockException
Goto StepStart
End Try
End Sub
Is it ok to follow this approach? Can someone give me an example in C# or VB.Net
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用这种方法就可以了。我们做了类似的事情。不过我有一点延迟,比如 500 毫秒。
使用SQL Server(和Sybase)锁定策略死锁总是会发生:你只能减少可能性按照 MSDN(2000 位仍然有效)。这也说要重试。
我还看到过重试的商业应用程序。
另一个选项是启用快照隔离,虽然并不完美
Yes, it's OK to use this approach. We do something similar. I'd a small delay though, say 500 ms.
With the SQL Server (and Sybase) locking strategy deadlocks will always happen: you can can only reduce the possibility as per MSDN (for 2000 bit still valid). Which also says to retry.
I've also seen commercial apps that retry too.
The other option is to enable snapshot isolation which is not perfect though