I have a database table containing a queue of work items processed concurrently by multiple readers, with these requirements:
- Each item should be processed by one reader only.
- If a reader fails for any reason, the item should be returned to the queue for another reader to process.
Here is the pseudo code of a possible solution, each reader would do the following:
1) Read next item from queue and store it locally somewhere.
2) START TRANSACTION
3) Delete the item, to prevent other readers from seeing it. If deletion fails, it means another worker already pulled the item so go back to step 1.
4) Process the item (stored locally in step 1). This could be long running.
5) COMMIT TRANSACTION : item deletion is committed, go to step 1 to process next item
OR
6) ROLLBACK TRANSACTION (explicit rollback or reader failure): item deletion is rolled back and returns back to queue for another reader
My question is: what is the lowest isolation level I need to ensure that after step 3 (item deletion), other readers cannot see it? By lowest, I mean I want to maximize concurrency while maintaining integrity.
Note I'm using SQL Server 2005, if it matters (I think this should be product agnostic, right?)
Any other feedback on this approach in general is welcome.