分布式程序对同一表进行操作,为了不使多个进程同时打嘴仗一条sql语句,可用下面的sql
@"declare @id int
UPDATE NorthBoundQueue SET LastProcessTime = getdate(),@id=t1.NorthBoundQueueID
FROM (SELECT TOP 1 * FROM NorthBoundQueue where DATEADD(millisecond, " + timeOut + @", LastProcessTime) < getdate() ORDER BY CreateTime ) AS t1
WHERE NorthBoundQueue.NorthBoundQueueID = t1.NorthBoundQueueID AND DATEADD(millisecond, 30000, NorthBoundQueue.LastProcessTime) < getdate()
SELECT TOP 1 * FROM NorthBoundQueue where NorthBoundQueue.NorthBoundQueueID = @id ";
本文介绍了一种使用SQL实现的分布式程序同步更新策略,通过特定的SQL语句确保多个进程在对同一表进行操作时不会发生冲突。该方法利用了数据库的事务特性,通过锁定记录并更新最后处理时间来避免并发冲突。
172万+

被折叠的 条评论
为什么被折叠?



