How to lock a single row
MySQL uses only table-level locking from MyISAM tables. If you can, switch to InnoDB for row-level locking.
Here's a link to the MySQL site describing Locks set by SQL Statements for InnoDB tables.http://dev.mysql.com/doc/refman/5.0/en/innodb-locks-set.html
Kind of late, but hope it will help someone:
UPDATE user SET lastusedecnumber = LAST_INSERT_ID(lastusedecnumber + 1);SELECT LAST_INSERT_ID();
Will give you atomic increment of lastusedecnumber and ability to read new value of lastusedecnumber
field (after increment) using SELECT LAST_INSERT_ID()
.
As a workaround you could add a column to your table, like locked TINYINT(1)
- whenever you want the row to be locked you set it to 1
. When you now try to access this row, the first thing you do is check if the locked
fields is set.
To unlock the row, simply set it to 0
again. Not nice but a really simple workaround.