I have a table with multiple rows. I also have a multithreaded application which reads a row with status = 1 and then changes it to status = 2 after reading.
However, due to the multithreaded nature of the application, it keeps reading the same row twice (by different threads). I know this is a concurrency problem but I am unable to solve it.
As of now, I am reading the row as:
SELECT TOP 1 * FROM Inbox WHERE Status = 1 ORDER BY ID DESC;
Then, using the ID, I update the row:
UPDATE Inbox SET Status = 2 WHERE ID = X;
I would like a query to lock a row as it selects its ID and returns it so no other threads are able to read it.