How do you lock tables in SQL Server 2005, and should I even do it?

后端 未结 7 1935
醉话见心
醉话见心 2021-02-19 15:35

This one will take some explaining. What I\'ve done is create a specific custom message queue in SQL Server 2005. I have a table with messages that contain timestamps for both a

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 16:11

    Instead of explicit locking, which is often escalated by SQL Server to higher granularity than desired, why not just try this approach:

    declare @MessageId uniqueidentifier
    select top 1 @MessageId = ActionMessageId from UnacknowledgedDemands
    
    update ActionMessages
      set AcknowledgedTime = getdate()
      where ActionMessageId = @MessageId and AcknowledgedTime is null
    
    if @@rowcount > 0
      /* acknoweldge succeeded */
    else
      /* concurrent query acknowledged message before us,
         go back and try another one */
    

    The less you lock - the higher concurrency you have.

提交回复
热议问题