更新时间:2022-04-09 01:13:58
在这种情况下,
这两个概念是不同的,你想要的也不是.
The 2 concepts are different and neither does what you want.
做你想做的事,避免竞争条件,需要强制一个非阻塞(READPAST)排他(UPDLOCK)行级(ROWLOCK)锁,.您还可以使用 OUTPUT 子句使其成为具有原子性的单个语句.这可以很好地扩展.
To do what you want, to avoid race conditions, you need to force a non-blocking (READPAST) exclusive (UPDLOCK) row level (ROWLOCK) lock,. You can also use the OUTPUT clause to make it a single statement that will be atomic. This scales well.
UPDATE
E
SET
[status] = 'PROCESSING', [username] = @Username
OUTPUT
INSERTED.*
FROM
(
SELECT TOP 1 id, [status], [username]
FROM Exceptions (ROWLOCK, READPAST, UPDLOCK)
WHERE [status] = 'READY'
ORDER BY id
) E
总的来说,锁有3个方面
In general, locks have 3 aspects
PAGLOCK、ROWLOCK、TABLOCK
)HOLDLOCK、READCOMMITTED、REPEATABLEREAD、SERIALIZABLE
)UPDLOCK, XLOCK
)和
NOLOCK, TABLOCKX