I have the following algorithm:
- Choose a row that has no answer and that is not locked (status 0)
- Lock this row to prevent others from getting it (status 1)
there is also status 2 - row has an answer and finished.
SELECT * FROM details WHERE RowStatus=0 AND taskid=".$task_id." ORDER BY RAND() LIMIT 1
UPDATE details SET RowStatus=1 ,Agent='".$_SESSION['username']."' where TaskID=".$row['TaskID']." and RowId=".$row['RowId']
The problem is that if i have 10 rows left and 8 agents working on the task, sometimes happens that they submit previous row in exactly the same time. Then both of them select the same row and only one of them locks the row with his name meaning - one of them is working on the same row.
Its sounds like a minor issue but somehow i got this happen 5-10 times in a 2500 rows task. Is there any way to lock the row in the same time as selecting it? Prevent other user to select this row?