Take the 2-minute tour ×
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.

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?

James

share|improve this question
add comment

1 Answer

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');
#IF RESULT IS 1
SELECT GET_LOCK('maintenance', '10');#it will get a lock named maintenance
#PERFORM ALL YOUR 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
add comment

Your Answer

 
discard

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.