Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find what rows are locked for a given table and who is locking them in SQL Server

Tags:

sql-server

Is there a way to find out what rows are locked for a specific table in SQL Server 2008? I'd also like to know the user who is locking them.

like image 447
Jeff Stock Avatar asked Sep 15 '11 20:09

Jeff Stock


People also ask

How do you check if there is a lock on a table in SQL Server?

You can use the sys. dm_tran_locks view, which returns information about the currently active lock manager resources. Awesome Query .

How do I find locked objects in SQL Server?

Expand server – management-currentActivity-expand Locks/object you can see locks by object information. Expand-server-management-double click Activity Monitor. on left side you have three options to choose from, select those options and you can see all the locks related information.

How do you find out who is locking a table in MySQL?

In MySQL, locked tables are identified using the SHOW OPEN TABLES command. In its simplest form is displays all locked tables. All open tables in the table cache are listed, but the IN_USE column indicates of the table is locked. When the first lock is taken, the value increments to 1.

How can I tell if a SQL row is locked?

Unfortunately, there is no query which will tell you if a row is currently locked. The only query is to query V$LOCK for a transaction lock, type TX. The ID1 and ID2 columns don't point directly to the row which the transaction is locked.


2 Answers

sys.dm_tran_locks, as already said in 694581. To identity which rows are actually locked, you need to understand the locking hierarchy (table->rowset->page->row) and you need to crack the lock resource description. For table locks is the object id from sys.objects, for rowsets is the partition_id from sys.partitions and for pages is the actual page id. For rows it depends whether is a heap or a btree, but you can use the (undocumented) %%lockres%% virtual column to find the row. If this is too simple, you need to consider also range locks as they impact all the rows in the specified range.

When you add up the difficulty of navigating the physical hierarchy, specially when page locks are involved, with the complex model of the lock compatibility matrix, the complications added by hash collisions and consider the the pace at which the locks you're looking at change, I would say that at the very best you can do a very rough approximation. Besides doing a specific problem investigation, there is little point into digging into this. I would be horrified to hear of an application that looks actively at locks held and makes any kind of decision based on the information seen.

like image 160
Remus Rusanu Avatar answered Oct 02 '22 08:10

Remus Rusanu


Here is an example how to find the main key of a locked records in a table:

SELECT <main_key>
FROM <table>
WHERE %%lockres%% IN ( select dm_tran_locks.resource_description from sys.dm_tran_locks )
like image 35
AGR Avatar answered Oct 02 '22 08:10

AGR