微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql-server – 如何查找锁定的表名(特定于任何事务)

有没有办法列出锁定的表并杀死交易,如果我们希望它们立即解锁.

或者我们需要遵循的任何其他术语,我正在寻找上述操作.

任何帮助或指导将不胜感激.

解决方法

这将使用 sys.dm_tran_locks DMV显示所有具有独占锁定的数据库(可能包括在运行时保留的瞬态锁定):
select d.*,l.* from sys.dm_tran_locks l
join sys.databases d on l.resource_database_id = d.database_id 
where l.request_mode = 'X'

(X =独占,S =共享,IS =意图共享)见Lock Modes.

但可能最好的方法是打开Trace Flags 1204和1222:

Trace Flag 1204 and Trace Flag 1222
When deadlocks occur,trace flag 1204
and trace flag 1222 return information
that is captured in the sql Server
2005 error log. Trace flag 1204
reports deadlock information formatted
by each node involved in the deadlock.
Trace flag 1222 formats deadlock
information,first by processes and
then by resources. It is possible to
enable both trace flags to obtain two
representations of the same deadlock
event.

参考:Detecting and Ending Deadlocks

另外,运行sp_who2并查找BlkBy(Blocked By)列中的条目;按照这些,直到你到达死锁链的头部.这是负责的进程标识符(或PID).

获取在特定进程后面运行的sql,您可以运行:

dbcc inputbuffer (@pid)

并使用该PID来杀死进程(谨慎并且风险自负):

kill @pid

查看Who is Active? v10.00: DMV Monitoring Made Easy

另请阅读Blocking is not Deadlocking(区分两种情况)

原文地址:https://www.jb51.cc/mssql/78922.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐