I'm using SqlDependency to monitor a table for changes. However, whenever SqlDependency begins monitoring a table, any updates to that table inside a transaction cause the entire table to get locked. If I run the exact same update without SqlDependency running, I only get a single key-level lock (request_mode=X?).
So for example, without the SqlDependeny running, I can do the following:
1) Open a window in Sql Server Management Studio (window #1)
2) Begin transaction
3) Update a single row in a table (named "Client").
4) Open another window in Sql Server Management Studio (window #2)
5) Begin transaction
6) Update a different row in the same table (named "Client")
7) Commit transaction
8) Switch back to window #1
9) Commit transaction
However, with the SqlDependency monitoring my table, step #6 hangs.
10) Run my app, which starts SqlDependency monitoring my "Client" table.
11) Repeat steps #1-#3.
12) Repeat steps #4-#5.
13) Step #6 hangs.
If I take a look at the dm_tran_locks table in the first case, after step #3, there are only 3 locks related to my update statement.
request_type request_mode ObjectName resourceType
LOCK IX Client OBJECTLOCK X N/A KEY
LOCK IX N/A PAGE
However, if I look at dm_tran_locks table after step #11 (with SqlDependency monitoring my table), I get tons of rows back.
In my opinion, the the key here is that the "KEY" lock is using request_mode "RangeX-X" now, instead of just "X".
LOCK Sch-M N/A METADATALOCK Sch-S N/A METADATA
LOCK Sch-S N/A METADATA
LOCK Sch-S N/A METADATA
LOCK Sch-S N/A METADATA
LOCK Sch-M N/A METADATA
LOCK X N/A METADATA
LOCK Sch-M N/A METADATA
LOCK Sch-S N/A METADATA
LOCK Sch-M N/A METADATA
LOCK X N/A METADATA
LOCK Sch-M N/A METADATA
LOCK X N/A METADATA
LOCK Sch-M N/A METADATA
LOCK IX sysxmitqueue OBJECT
LOCK IX sysconvgroup OBJECT
LOCK IX sysdesend OBJECT
LOCK IX sysdercv OBJECT
LOCK IX queue_messages_87267666 OBJECT
LOCK IX query_notification_299408386 OBJECT
LOCK IX query_notification_299408386 OBJECT
LOCK IX Client OBJECT
LOCK IX Client OBJECT
LOCK IX query_notification_1641317157 OBJECT
LOCK IX query_notification_1641317157 OBJECT
LOCK X N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK RangeX-X N/A KEY
LOCK IU N/A PAGE
LOCK IX N/A PAGE
LOCK RangeS-U N/A KEY
LOCK RangeX-X N/A KEY
LOCK RangeS-U N/A KEY
LOCK X N/A KEY
LOCK X N/A KEY
LOCK IX N/A PAGE
LOCK IX N/A PAGE
LOCK X N/A KEY
LOCK X N/A KEY