互斥存储过程

发布于 2024-07-17 04:32:50 字数 286 浏览 7 评论 0原文

我想使用数据库表创建一些分布式互斥。 如果存储过程具有以下接口,那就太好了:

Wait(uniqueidentifier)

我最初考虑通过拥有唯一标识符表来实现此功能。 对该过程的调用将等到表中不存在唯一标识符为止。 但是,我不确定当从表中删除指定的唯一标识符时如何唤醒调用线程。

有任何想法吗? 如果数据库不是执行此操作的正确位置,是否有任何可以使用的第三方工具(最好是开源的)?

(为了避免死锁,我要么想在等待操作中包含超时,要么让 SqlCommand 有超时)

I want to create some distributed mutual exclusion using a database table. It would be nice to have the following interface on a stored procedure:

Wait(uniqueidentifier)

I was originally thinking of implementing this by having a table of unique identifiers. A call to the procedure would wait until the unique identifier does not exist in the table. However, I'm not sure how I would make the calling thread wake up when the specified unique identifier was removed from the table.

Any ideas? If the database is not the right place to do this, are there any third party tools that would work (open source preferably)?

(To avoid deadlocks, I either want to include a timeout in the wait operation or have the SqlCommand have a timeout)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2024-07-24 04:32:50

看一下系统存储过程:

 sp_getapplock

它可能会帮助您完成您想要做的事情。

http://msdn.microsoft.com/en-us/library/ms189823。 aspx

您可以将其放入参数化 uniqueidentifier 的过程中...

BEGIN TRAN

DECLARE @result int

EXEC @result = sp_getapplock @Resource = 'YOUR_uniqueidentifier_HERE', 
                             @LockMode = 'Exclusive',
                             @LockTimeout = 90

IF @result NOT IN ( 0, 1 )   -- Only successful return codes
BEGIN
  PRINT @result
  RAISERROR ( 'Lock failed to acquire...', 16, 1 )
END 
ELSE
BEGIN
    -- DO STUFF HERE 
END
EXEC @result = sp_releaseapplock @Resource = 'YOUR_uniqueidentifier_HERE'  

COMMIT TRAN

Take a look at the system stored procedure:

 sp_getapplock

It may help you accomplish what you are trying to do.

http://msdn.microsoft.com/en-us/library/ms189823.aspx

You can put it in a proc that parametrizes the uniqueidentifier...

BEGIN TRAN

DECLARE @result int

EXEC @result = sp_getapplock @Resource = 'YOUR_uniqueidentifier_HERE', 
                             @LockMode = 'Exclusive',
                             @LockTimeout = 90

IF @result NOT IN ( 0, 1 )   -- Only successful return codes
BEGIN
  PRINT @result
  RAISERROR ( 'Lock failed to acquire...', 16, 1 )
END 
ELSE
BEGIN
    -- DO STUFF HERE 
END
EXEC @result = sp_releaseapplock @Resource = 'YOUR_uniqueidentifier_HERE'  

COMMIT TRAN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文