I am using SELECT...FOR UPDATE
queries to lock a table. Unfortunately, I have a situation where I need to lock two different sets of rows in the same table, like so:
SELECT * FROM mytable WHERE attribute1 = 'something' FOR UPDATE
SELECT * FROM mytable WHERE attribute2 = 'somethingelse' FOR UPDATE
UPDATE mytable SET attribute2 = 'somethingnew' WHERE somethingelse
I need to lock both sets of rows. What I'm doing is, making sure none of the objects in the table are in particular state, and then finding other objects which can be put into that state and putting them there. The goal is to satisfy certain constraints which are too complex to be encoded into mysql.
So... the question is, what happens when I SELECT...FOR UPDATE
twice from the same table inside the same transaction? Does the first lock get released? I've seen evidence that this is the case, but I can't quite figure out how to confirm.