有时我在不太繁忙的 SQL 服务器上遇到这种异常:
Transaction (Process ID 57) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Line number: 1
Error Number: 1205
Procedure:
Server name: P01
Error Source: .Net SqlClient Data Provider
Error State: 47
我无法重现它。我尝试同时运行来自不同客户端的多个查询,但它没有显示。
当这种问题发生在过程或触发器内部时,处理此类问题的最佳方法是什么?我的意思是,如何重新运行交易?
当从触发器调用的过程内部发生异常时如何处理,该异常是由某个过程的插入调用的(即: procedure01 -> insert -> trigger -> procedure02 !)
Sometimes I get this kind of exception on not very busy SQL server:
Transaction (Process ID 57) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Line number: 1
Error Number: 1205
Procedure:
Server name: P01
Error Source: .Net SqlClient Data Provider
Error State: 47
I'm not able to reproduce it. I tried to run several queries from different clients at the same time, but it didn't show up.
What is the best way to handle this kind of issue when it happens inside procedure or inside trigger? I mean, how to rerun the transaction?
How to do it when exception occurs inside procedure called from the trigger, which was called by the insert made by some procedure (that is: procedure01 -> insert -> trigger -> procedure02 !)
发布评论
评论(4)
我建议你从两个角度来看待这个问题。
陷阱或捕获死锁错误以便您可以重新运行被 SQL Server 数据库引擎选为死锁受害者的事务。
找出导致死锁事件的原因。
您可以通过以下两种方式之一执行此操作:运行 SQL Server Profiler Trace 来捕获并记录死锁事件,或者启用一些SQL Server 跟踪标志,它将死锁事件的详细信息记录到 SQL Server 错误日志中。
在绝大多数情况下,您可以确定死锁事件的原因,并通过数据库架构中的结构更改或对涉及/负责死锁事件的代码进行逻辑更改来补救这种情况。
要进一步阅读,请查看:
I希望我已经回答了您的问题,但如果我可以以任何方式进一步帮助您,请告诉我。
I would suggest that you come at the problem from two perspectives.
Trap or Catch Deadlock Errors so that you can re-run the transaction that is chosen as the Deadlock Victim by the SQL Server database engine.
Find out what is causing your Deadlock Events.
You can do this in one of two ways, either run a SQL Server Profiler Trace to catch and record the Deadlock Event or you can enable some SQL Server Trace Flags that will record the details of the Deadlock Event to the SQL Server Error Log.
In the vast majority of cases, you can identify the cause of your Deadlock Events and remedy the situation through either a structural change in the database schema or a logical change to the code involved/responsible for the Deadlock Event.
For further reading take a look at:
I hope I've answered your question but do let me know if I can help you further in any way.
设置捕获死锁图事件的服务器端 SQL 跟踪,以便您可以使用 SQL Profiler 查看 .trc 文件。这样您就可以采取一些措施来解决任何死锁问题。我提供了下面的代码。您必须根据需要更改文件路径。最好将此脚本配置为在 SQL Server 启动时执行。
仅供参考 - 很多不同的事情都可能导致死锁,其中之一就是缺少索引。
Set up a server-side SQL trace which captures deadlock graph events so you can look at the .trc file with SQL Profiler. This way you can have something in place to be able to troubleshoot any deadlock. I have provided the code below. You will have to change the file path as appropriate. It would be a good idea to configure this script to execute on SQL server startup.
FYI - A lot of different things can cause a deadlock, one of them being missing indexes.
我使用 ReadCommited 隔离级别解决了类似的问题。
I solved like problem using ReadCommited isolation level.
我通过以下方法解决了此错误:
运行查询:
使用[主控]
更改数据库名称_数据库
设置多用户
立即回滚
祝你好运!
I am resolved this error by:
Run Query:
USE [master]
ALTER DATABASE name_database
SET MULTI_USER
WITH ROLLBACK IMMEDIATE
Goodluck!