Reputation: 37337
I have following scenraio:
I need to fire updating procedures in fixed time intervals. But some of the procedure might take longer than the interval. I want to avoid stacking calls to procedures (in case one want to start before its previous execution hasn't been finished).
My colleague advised me to create in database additional table with two columns:
one with name of procedures and IsActive
column (bit
). So, before executing any procedure, I check the corresponding value of IsActive
. If it's 1
, then abort execution.
Now, the problem:
when I get to execution, I need to set the value of IsActive
to 1
for the procedure, which I try to do like this:
UPDATE ProcActivity SET IsActive = 1 WHERE ProcedureName = 'proc_name'
EXEC proc_name
UPDATE ProcActivity SET IsActive = 0 WHERE ProcedureName = 'proc_name'
But, SQL is executing batches, so the value of 1
isn't visible (the UPDATE
isn't commited) until the procedure is finished.
So, how to commit this UPDATE
? I tried with COMMIT
, but didn't work... I can't use GO
, because it's wrapped in IF
statement...
Upvotes: 0
Views: 4151
Reputation: 432180
Don't use transactions this way because of visibility, unless you want to use extra hints. WHich I would not do personally.
If you want "only one stored proc execution current" then I would consider sp_getapplock
and sp_releaseapplock
in the stored procedure.
This will enforce force "single threaded" execution.
Other questions here that show how to use it
And, to abort other calls, set the @LockTimeout=0
, so if the result code is different from zero, then you know that you need to abort current call.
Upvotes: 4