I am writing a call centre program in C# where multiple agents load the customers one by one from a table. In order to prevent more than one agent to load the same customer, I have added a new field to the table to show that the row is locked. When I select a row, I update that row and set the lock field to the ID of the agent who has selected that row. But the problem is during the time that I select the row and I lock it, another agent can select the same row since it’s not locked yet ! Is there a way I can handle this situation ? The database is MySQL 5 / InnoDB
Share
Assuming you can only lock 1 profile per agent:
If you do the UPDATE in 1 statement, you don’t. We’re getting a little past my knowledge of MySQL syntax – but something like:
works pretty easily.
If you want to get a little more complicated (or MySQL doesn’t support the subquery), you can use an update lock with SELECT…FOR UPDATE: