简体   繁体   English

SQL Server 2008 R2 查询通知上的死锁 (SqlDependency)

[英]SQL Server 2008 R2 DeadLock on query Notification (SqlDependency)

I'm running into a deadlock issue on a SQL Server 2008 R2.我在 SQL Server 2008 R2 上遇到了死锁问题。 When looking at the deadlock graph in the SQL Profiler, the problem seems to stem from query notifications :查看 SQL Profiler 中的死锁图时,问题似乎源于查询通知:

  <resource-list>
   <keylock hobtid="72057654759522304" dbid="6" objectname="MyDB.sys.query_notification_814081939" indexname="cidx" id="lock15ab2aa80" mode="RangeX-X" associatedObjectId="72057654759522304">
    <owner-list>
     <owner id="process5c5708" mode="RangeX-X"/>
    </owner-list>
    <waiter-list>
     <waiter id="process4e9ae08" mode="RangeS-U" requestType="wait"/>
    </waiter-list>
   </keylock>
   <keylock hobtid="72057654759522304" dbid="6" objectname="MyDB.sys.query_notification_814081939" indexname="cidx" id="lock15e56a300" mode="RangeS-U" associatedObjectId="72057654759522304">
    <owner-list>
     <owner id="process4e9ae08" mode="RangeS-U"/>
    </owner-list>
    <waiter-list>
     <waiter id="process5c5708" mode="RangeS-U" requestType="wait"/>
    </waiter-list>
   </keylock>
  </resource-list>

These query notifications are implemented using SQLDependency.这些查询通知是使用 SQLDependency 实现的。 The deadlock seems to be occuring when updating a table which is monitored by the SQLDependency.更新 SQLDependency 监视的表时似乎发生死锁。

I don't really understand the deadlock graph syntax..The KeyLock mode is RangeS-U only while using the serializable transaction isolation level.我不是很懂死锁图的语法。KeyLock 模式只有在使用可序列化事务隔离级别时才是 RangeS-U , right ? , 对 ?

I've also read this question ... should i activate READ_COMMITED_SNAPSHOT ?我也读过这个问题……我应该激活 READ_COMMITED_SNAPSHOT 吗?

thx...谢谢...

Can't pin it down for sure, but here are some thoughts...不能确定,但​​这里有一些想法......

The statement that causes the notification may not complete before the notification is delivered.在传递通知之前,导致通知的语句可能不会完成。 Thus it may still have active locks at the point when the notification is received from service broker and any action is taken on the notification.因此,在从服务代理接收到通知并对通知采取任何操作时,它可能仍然具有活动锁。

Maybe your notification recipient is trying to clean up the queue or get a second notification out of the queue before the transaction that generated the first notification is complete.也许您的通知接收者正在尝试清理队列或在生成第一个通知的事务完成之前从队列中获取第二个通知。

Is the DML that generated the notification running in a multi-step transaction?生成通知的 DML 是否在多步事务中运行? Is the code that receives notifications running in a multi-step transaction.是在多步事务中运行的接收通知的代码。 (ie did you use begin tran or equivalent?). (即您是否使用了 begin tran 或等效的?)。

It could be useful to track down the processes mentioned in the deadlock graph and understand which code holds the RangeX-X lock and which holds the RangeS-U.跟踪死锁图中提到的进程并了解哪些代码持有 RangeX-X 锁以及哪些代码持有 RangeS-U 可能很有用。

You might want to post some minimal examples of the code that generates a notification and the code that receives it.您可能希望发布一些生成通知的代码和接收通知的代码的最小示例。

Also here is a Microsoft KB about a known somewhat similar deadlock problem with notifications and multiple subscriptions.这里还有一个 Microsoft KB,关于通知和多个订阅的已知有点类似的死锁问题。

MS KB975090 Might be relevant, but not exactly the same. MS KB975090 可能相关,但不完全相同。

The main cause behind that is the fact that transactions are too long and involve too many objects.这背后的主要原因是事务太长并且涉及太多对象。

As I said in my question, the table on which the SqlDependency is fired contains a reference to each object in the database.正如我在我的问题中所说,触发 SqlDependency 的表包含对数据库中每个对象的引用。 Updating a bunch of objects in the same transaction implies locks on this big table by the query notification system.在同一个事务中更新一堆对象意味着查询通知系统锁定了这个大表。 Hence, you're quickly trapped in a deadlock situation.因此,您很快就会陷入僵局。

2 solutions (advised by MS): 2个解决方案(由MS建议):

  • Reduce the length and complexity of transactions.减少事务的长度和复杂性。
  • Execute the requests triggering the notification system outside big transactions.在大事务之外执行触发通知系统的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM