这个将需要一些解释.我所做的是在SQL Server 2005中创建一个特定的自定义消息队列.我有一个表包含消息,其中包含确认和完成的时间戳.调用程序执行以获取其队列中的下一条消息的存储过程也会确认该消息.到现在为止还挺好.那么,如果系统正在经历大量的事务(每分钟数千个),那么另一个执行存储过程的消息是否可能被确认而另一个本身就准备好了?让我通过在存储过程中显示我的SQL代码来帮助:
--Grab the next message id
declare @MessageId uniqueidentifier
set @MessageId = (select top(1) ActionMessageId from UnacknowledgedDemands);
--Acknowledge the message
update ActionMessages
set AcknowledgedTime = getdate()
where ActionMessageId = @MessageId
--Select the entire message
...
...
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,同时运行的另一个存储过程是否可以获得相同的id并尝试同时确认它?我(或我应该)可以实现某种锁定以防止另一个存储过程确认另一个存储过程正在查询的消息吗?
哇,这有甚么有意义吗?说文字有点困难......
像这样的东西
--Grab the next message id
begin tran
declare @MessageId uniqueidentifier
select top 1 @MessageId = ActionMessageId from UnacknowledgedDemands with(holdlock, updlock);
--Acknowledge the message
update ActionMessages
set AcknowledgedTime = getdate()
where ActionMessageId = @MessageId
-- some error checking
commit tran
--Select the entire message
...
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29168 次 |
| 最近记录: |