SQL Server: Retry update if fail
-
create table PendingQueue (
id int not null,
DueTime datetime not null,
Payload varbinary(max),
cnstraint pk_pending_id nonclustered primary key(id));create clustered index cdxPendingQueue on PendingQueue (DueTime);
gocreate procedure usp_enqueuePending
@dueTime datetime,
@payload varbinary(max)
as
set nocount on;
insert into PendingQueue (DueTime, Payload)
values (@dueTime, @payload);
gocreate procedure usp_dequeuePending
@batchsize int = 100,
@retryseconds int = 600
as
set nocount on;
declare @now datetime;
set @now = getutcdate();
with cte as (
select top(@batchsize)
id,
DueTime,
Payload
from PendingQueue with (rowlock, readpast)
where DueTime < @now
order by DueTime)
update cte
set DueTime = dateadd(seconds, @retryseconds, DueTime)
output deleted.Payload, deleted.id;
goSpecially see this line set DueTime = dateadd(seconds, @retryseconds, DueTime) How this line instruct sql server to retry the update data again after 10 minute if fail to update data first time. this is not clear to me. please some one explain if you understand properly. thanks
-
create table PendingQueue (
id int not null,
DueTime datetime not null,
Payload varbinary(max),
cnstraint pk_pending_id nonclustered primary key(id));create clustered index cdxPendingQueue on PendingQueue (DueTime);
gocreate procedure usp_enqueuePending
@dueTime datetime,
@payload varbinary(max)
as
set nocount on;
insert into PendingQueue (DueTime, Payload)
values (@dueTime, @payload);
gocreate procedure usp_dequeuePending
@batchsize int = 100,
@retryseconds int = 600
as
set nocount on;
declare @now datetime;
set @now = getutcdate();
with cte as (
select top(@batchsize)
id,
DueTime,
Payload
from PendingQueue with (rowlock, readpast)
where DueTime < @now
order by DueTime)
update cte
set DueTime = dateadd(seconds, @retryseconds, DueTime)
output deleted.Payload, deleted.id;
goSpecially see this line set DueTime = dateadd(seconds, @retryseconds, DueTime) How this line instruct sql server to retry the update data again after 10 minute if fail to update data first time. this is not clear to me. please some one explain if you understand properly. thanks
It doesn't. It simply updates the
DueTime
column in thePendingQueue
table, and returns thePayload
andid
columns of the affected rows. There must be some other code which processes and removes the record from thePendingQueue
table which you haven't shown.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer