I have a table which is a list of work to do, and multiple processes looking at the table to get the next task. It takes a relatively long time to finish the task, and after it is finished the process marks the task as completed. Only one process should find and handle a task. While a task is being handled it is marked as busy. So tasks are ready, busy, and completed.
It is the transition from ready to busy that concerns me. If process A gets the oldest task which is ready and marks it as busy, I need to prevent process B from finding the same task before process A has marked it as busy.
I am thinking this can be handled by a transaction, but do I need to do more such as using a locking hint for the read?
It is the transition from ready to busy that concerns me. If process A gets the oldest task which is ready and marks it as busy, I need to prevent process B from finding the same task before process A has marked it as busy.
I am thinking this can be handled by a transaction, but do I need to do more such as using a locking hint for the read?
Code:
BEGIN TRANSCACTION
SELECT @nextTask = MIN(task_id) FROM WorkToDO
WHERE status = 'Ready' [COLOR=blue]WITH (READPAST)[/color]
UPDATE WorkTodo SET status = 'Busy'
WHERE task_id = @nextTask
COMMIT TRANSACTION