troubleshooting Question

howto add locks into mysql table

Avatar of msifox
msifoxFlag for Germany asked on
MySQL Server
12 Comments1 Solution391 ViewsLast Modified:
Hi experts,

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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros