Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction

前端 未结 5 1934
耶瑟儿~
耶瑟儿~ 2020-12-30 00:47

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 cal

5条回答
  •  被撕碎了的回忆
    2020-12-30 01:26

    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:

    • 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.

    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.

提交回复
热议问题