I want to prevent that two people edit the same data record simultaneously.
One idea is to make a mysql table 'locks' with fields
tablename, entryid, userid, timestamp
and to add an entry whenever a user starts editing a record (and remove it when the edited form is submitted, also each time remove all entries that are older than one hour).
But I'm uncertain what's precisely the best method do do this:
I could make 'tablename,entryid' the primary key and try to add an entry.
If this fails then this entry is already locked (or some other error happened).
Or I could append another entry and then count if the number of entries with
this combination of tablename,entryid is 1.
Is there a reason to prefer one method over the other? Race conditions possible?
Or is there a completely different, better method to handle this?