模式处理C#中的SQL僵局? [英] Patterns for handling a SQL deadlock in C#?

查看:95
本文介绍了模式处理C#中的SQL僵局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中其中一个访问SQL Server 2005数据库的应用程序。该应用程序是相当密集的数据库,即使我尝试优化所有的访问,建立适当的索引等我希望我会得到死锁迟早的事。我知道为什么数据库死锁发生,但我怀疑我能不死锁在一段时间存在的释放软件。该应用程序是使用实体框架的数据库访问

I'm writing an application in C# which accesses a SQL Server 2005 database. The application is quite database intensive, and even if I try to optimize all access, set up proper indexes and so on I expect that I will get deadlocks sooner or later. I know why database deadlocks occur, but I doubt I'll be able to release the software without deadlocks occuring at some time. The application is using Entity Framework for database access.

是否有处理的SQLException(僵持)在C#客户端代码进行任何的良好格局 - 例如重新运行该语句?在x毫秒为单位批量

Are there any good pattern for handling SQLExceptions (deadlocked) in the C# client code - for example to re-run the statement batch after x milliseconds?

要澄清;我不是在寻找如何避免在首位的死锁(隔离级别,指标,报表等的顺序),而是如何在实际发生处理它们的方法。

To clarify; I'm not looking for a method on how to avoid deadlocks in the first place (isolation levels, indexes, order of statements etc) but rather how to handle them when they actually occur.

推荐答案

我贴了代码示例来处理正是这个而回,但SO似乎失去我的帐户在此期间,所以我不能找到它现在我害怕, 。不用我在这里使用的代码

I posted a code sample to handle exactly this a while back, but SO seemed to lose my account in the interim so I can't find it now I'm afraid and don't have the code I used here.

简短的回答 - 包裹的东西在try..catch。如果你发现它看起来像一个僵局,睡眠很短的随机时间错误,并增加重试计数器。如果你得到另一个错误或重试计数器清零的阈值时,抛出该错误恢复到调用程序。

Short answer - wrap the thing in a try..catch. If you catch an error which looks like a deadlock, sleep for a short random time and increment a retry the counter. If you get another error or the retry counter clears your threshold, throw the error back up to the calling routine.

(如果可以的话,尝试在以塞子此一般常规和运行大多数/通过它所有的数据库访问,所以你正在处理死锁计划范围内)

(And if you can, try to bung this in a general routine and run most/all of your DB access through it so you're handling deadlocks program-wide.)

编辑:啊,教我不要用谷歌!上面的代码示例中,我和其他人,放弃在的 http://stackoverflow.com/questions/320636/how-to-get-efficient-sql-server-deadlock-handling-in-c-with-ado

Ah, teach me not to use Google! The previous code sample I and others gave is at http://stackoverflow.com/questions/320636/how-to-get-efficient-sql-server-deadlock-handling-in-c-with-ado

这篇关于模式处理C#中的SQL僵局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆