How To Find The Table Names Which Are Locked (Specific to any transaction)

后端 未结 1 1597
梦谈多话
梦谈多话 2021-02-02 03:10

Is there any way to list out the locked tables and to kill the transactions if we want them to be unlocked immediately.

Or is there any other terminology do we need to f

1条回答
  •  独厮守ぢ
    2021-02-02 04:04

    This will show all databases with exclusive locks being held (which may include transient ones held at the time this is run), using the 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 = exclusive, S = Shared, IS = Intent Shared) See Lock Modes.

    But probably the best way is to turn on Trace Flags 1204 and 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.

    Ref: Detecting and Ending Deadlocks

    Also, run sp_who2 and look for entries in the BlkBy (Blocked By) column; follow these until you get to the head of the deadlock chain. That is the process identifier (or PID) responsible.

    To get what sql is running behind a specific process you can run:

    dbcc inputbuffer (@pid)
    

    and use that PID to kill the Process (with prudence and at your own risk):

    kill @pid
    

    Check out Who is Active? v10.00: DMV Monitoring Made Easy

    Also read Blocking is not Deadlocking (to distinguish the two scenarios)

    0 讨论(0)
提交回复
热议问题