事务(进程ID)已被死锁的锁资源与另一个进程,并已被选作死锁牺牲品。重新运行该事务 [英] Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction

查看:1793
本文介绍了事务(进程ID)已被死锁的锁资源与另一个进程,并已被选作死锁牺牲品。重新运行该事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是将数据插入到SQL Server中使用存储过程(2008年)表中的C#应用​​程序。我使用多线程来做到这一点。该存储过程是从该线程中调用。
现在我的存储过程是使用TABLOCK,而插入数据。
在执行此代码我收到以下错误:
。事务(进程ID)已被死锁的锁资源与另一个进程,并已被选作死锁牺牲品重新运行该事务

I have a C# application which is inserting data into SQL Server (2008) table using stored procedure. I am using multi-threading to do this. The stored procedure is being called from inside the thread. Now my stored procedure is using "tablock" while inserting data. While executing this code I am getting the following error: "Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction."

任何人都可以请帮助我的任何解决这个?

Can anyone please help me with any solution to this?

推荐答案

这发生当两个SQL Server进程正在访问相同的资源,但在不同的顺序。因此它们最终都等待其他过程,这是一个死锁

This occurs when two Sql Server processes are accessing the same resources, but in a different order. Therefore they end up both waiting for the other process, which is a deadlock.

有多种方式,以防止它,包括:

There are a number of ways to prevent it, including:


  • 避免不必要的服用锁。复查查询所需的事务隔离级别,使用与(NOLOCK)锁定提示的查询在适当情况下。

  • 确保当带您采取在每个查询相同的顺序对象的锁锁。

  • Avoid taking unneccessary locks. Review the transaction isolation level required for the query, use with (nolock) locking hint for queries where appropriate.
  • Make sure that when taking locks you take locks on objects in the same order in each query.

例如:如果PROC1锁表1和表2中,然后,但PROC2锁表2和表1然后,可能出现的问题。你可以重写任一进程内采取锁以相同的顺序来避免这个问题。

E.g. if Proc1 locks table1 and then table2, but Proc2 locks table2 and then table1, the problem can arise. You can rewrite either proc to take locks in the same order to avoid this problem.

这篇关于事务(进程ID)已被死锁的锁资源与另一个进程,并已被选作死锁牺牲品。重新运行该事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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