mysqltransactionsinnodbrowlocking

Is it neccessary to unlock a table after a commit while row level locking?


I'm writing some code that uses row level locking with MySQL (innodb backend).

The pseudocode is:

START TRANSACTION
SELECT * FROM foo WHERE foocondition FOR UPDATE
UPDATE foo set bar=value WHERE foocondition
COMMIT

I can't find on the mysql documentation info on the locks held AFTER the commit.

Do I have to execute a "UNLOCK TABLES" after the COMMIT or is it implicit? The answer should be "NO", but I'd like to have feedback on that.


Solution

  • UNLOCK TABLES has nothing to do with locking reads that you are using in your example. You use UNLOCK TABLES when you have previously locked a table with LOCK TABLES command. Even if you use locking reads to set range locks so that no one can INSERT new rows to the table, the table itself is not locked.

    Locks set by a locking read like SELECT ... FOR UPDATE and DML statements like UPDATE ... WHERE are released automatically when the transaction ends.