I have concerns on preventing concurrent writes of the same data. (PHP + MySQL)
I have a simple flow:
SELECT a record -> process the data in PHP -> UPDATE the record
My nightmare scenario (see example below) is User1 and User2 both pull the same record out of the database, and both change and write the record back... Leading to one of the users' changes being washed out without being seen.
Is it possible to run PHP in "single threaded" mode, which will queue the actions of the two users?
Thanks !
Edit:
To make things more clear, here is the scenario I want to avoid:
- User 1 gets data v1 from DB (SELECT)
- User 2 gets data v1 from DB (SELECT)
- User 1 generates data v1.1 (PHP)
- User 2 generates data v1.1' (PHP)
- User 1 updates data (UPDATE: v1.1 is in DB)
- User 2 updates data and erases data v1.1 (UPDATE: v1.1' is in DB)
So I want to make sure that before User 2 gets the data, User 1 has updated it. I do not know whether this is the natural order of things and I have nothing to do, or if I need to put some lock in place.