Preventing scripts from simultaneously accessing a table
Posted: Fri Apr 15, 2005 4:57 pm
Hello
I use MySQL and I have a problem with my script. First I obtain some data from a table (with a SELECT query) then I do something with it and update the field the data was taken from (with an UPDATE query). It works fine as long as the script is not run twice almost at the same time, because then the following problem arises:
1. Script 1 obtains data
2. Script 2 obtains data
3. Script 1 modifies the data
4. Script 2 modifies the data
5. Script 1 writes the modified data
6. Script 2 writes the modified data
The problem is that the data is modified only once (the data obtained is the same, and the result is the same for both scripts), not twice, as it should be. I tried to use LOCK TABLES (with READ, WRITE and READ WRITE), but it didn't work. Does anyone know how to lock a table so that two or more scripts cannot access it at the same time?
Thanks
I use MySQL and I have a problem with my script. First I obtain some data from a table (with a SELECT query) then I do something with it and update the field the data was taken from (with an UPDATE query). It works fine as long as the script is not run twice almost at the same time, because then the following problem arises:
1. Script 1 obtains data
2. Script 2 obtains data
3. Script 1 modifies the data
4. Script 2 modifies the data
5. Script 1 writes the modified data
6. Script 2 writes the modified data
The problem is that the data is modified only once (the data obtained is the same, and the result is the same for both scripts), not twice, as it should be. I tried to use LOCK TABLES (with READ, WRITE and READ WRITE), but it didn't work. Does anyone know how to lock a table so that two or more scripts cannot access it at the same time?
Thanks