我有一个表,sys_QueueJob
存储队列逻辑数据。
我认为更新并返回就足够了...但是,现在我不确定这是否 100% 安全。
我如何确定无论有多少并行请求都不会返回相同的 ID?
UPDATE sys_QueueJob
SET ExecutionStartedOn = GETDATE()
OUTPUT DELETED.Id as Result
WHERE Id = (select top 1 x.Id
from sys_QueueJob x with (rowlock, updlock, readpast)
where x.ExecutionFinishedOn is null
AND (
x.ExecutionStartedOn is null
OR x.ExecutionStartedOn < DATEADD(HOUR, -1, GETDATE())
)
order by x.CreatedOn asc)