Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a section of code that locks the database, updates it and then confirms.

This is all working fine, if another user attempts to update the same row they cannot and their changes are discarded.

My problem is that there is a variable that is updated each time the query is run, this increases whether the query was successful or not. So what I need is the query NOT to run in the first place if the row is locked, how do I do this?


share|improve this question
up vote 0 down vote accepted

you can use GET_LOCK() to avoid the query to run if other query is running on the same lock name.

you will basically do:

SELECT IS_FREE_LOCK('maintenance');
SELECT GET_LOCK('maintenance', '10');#it will get a lock named maintenance
SELECT RELEASE_LOCK('maintenance'); #it will release a lock named maintence

what will happen is that the thread 1 will get a lock named maintenance, while this thread don't release this lock, the command SELECT IS_FREE_LOCK('maintenance'); won't return 1.

share|improve this answer
Hope this question doesn't sound too stupid - BEGIN and COMMIT would be in the "#PERFORM ALL YOUR MAINTENANCE" area? – James Clifton Apr 23 '13 at 4:25
Yes, if you are using innoDB, you should use transactions (BEGIN and COMMIT). – altmannmarcelo Apr 23 '13 at 9:22
Thanks for that :) – James Clifton Apr 23 '13 at 23:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.