简体   繁体   English

如何防止成为僵局受害者?

[英]How can I prevent becoming a deadlock victim?

I got this exception msg: 我收到此异常消息:

Transaction (Process ID 55) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. 事务(进程ID 55)与另一个进程在锁资源上死锁,因此已被选择为死锁受害者。

The only line of my code that was implicated in the Stack Trace was the last one here: 我的代码中唯一包含在堆栈跟踪中的行是这里的最后一行:

public static DataTable ExecuteSQLReturnDataTable(string sql, CommandType cmdType, params SqlParameter[] parameters)
{
    using (DataSet ds = new DataSet())
    using (SqlConnection connStr = new SqlConnection(CPSConnStr))
    using (SqlCommand cmd = new SqlCommand(sql, connStr))
    {
        cmd.CommandType = cmdType;
        cmd.CommandTimeout = EXTENDED_TIMEOUT;
        foreach (var item in parameters)
        {
            cmd.Parameters.Add(item);
        }

        try
        {
            cmd.Connection.Open();
            new SqlDataAdapter(cmd).Fill(ds);

This is a general-purpose method that I use for all sorts of queries; 这是我用于各种查询的通用方法。 I haven't changed it recently, nor have I ever seen this particular exception before. 我最近没有更改它,也从未见过这种特殊的异常。

What can I do to guard against this exception being thrown again? 我怎样做才能防止再次引发此异常?

You can catch the deadlock exception and retry X number of times before giving up. 您可以捕获死锁异常,并在放弃前重试X次。

There's no magic solution to avoid deadlocks. 没有避免死锁的神奇解决方案。 If SQL Server detects a deadlock it's going to pick one of the processes to kill. 如果SQL Server检测到死锁,它将选择要杀死的进程之一。 In some cases you may have had deadlocks where your process was the one that was lucky enough to continue. 在某些情况下,您可能遇到了死锁,而您的过程才是幸运的,可以继续下去。

You can use SQL Profiler to capture the deadlocks. 您可以使用SQL事件探查器捕获死锁。 I had to do this in the past to try and figure out what was actually causing the deadlocks. 过去,我不得不这样做,以找出造成僵局的真正原因。 The less often this happens the harder it is to track down. 这种情况发生的次数越少,越难追踪。 In our test environment we just created some testing code to hammer the database from a few different machines to try and cause a deadlock. 在我们的测试环境中,我们只是创建了一些测试代码来锤击来自不同机器的数据库,以尝试导致死锁。

In our case we made some changes to our indexes and modified database triggers to reduce the deadlocks as best we could. 在我们的案例中,我们对索引进行了一些更改,并修改了数据库触发器,以最大程度地减少死锁。 In the end we still had to implement the retries as a "just in case". 最后,我们仍然必须以“以防万一”的方式执行重试。

It might have helped if you had shown the SQL that was passed to ExecuteSQLReturnDataTable . 如果您显示了传递给ExecuteSQLReturnDataTable的SQL,可能会有所帮助。 Meanwhile read Minimizing Deadlocks . 同时阅读最小化死锁

Of course, you may have to also look at whatever else is contributing to the deadlock. 当然,您可能还必须研究导致僵局的其他因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止int减去后变成负数 - How do I prevent an int from becoming negative after subtracting 如何防止用户管理器死锁? - How to prevent Deadlock with Usermanager? 在简单的Add()-> SaveChanges()上被选为死锁受害者 - Chosen as the deadlock victim on simple Add() -> SaveChanges() 这可以成为SQL注入的受害者吗 - Can this be victim of sql injection 同步到异步调度:如何避免死锁? - Sync to async dispatch: how can I avoid deadlock? 如何防止自引用表变为循环 - How to prevent a self-referencing table from becoming circular 如何防止标签被空格分隔? - How to prevent tags from becoming separated by white-space? 如何防止MDI子窗口被“工具栏”夹住? - How can I keep an MDI child window from becoming “trapped” underneath a toolbar? 事务(进程ID)已与另一个进程在锁资源上发生死锁,因此已被选择为死锁受害者。 重新运行交易 - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction 如何防止死锁以及如何在实体框架中设置隔离级别? - How to prevent deadlock and how to set isolation level in entity framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM