6

无论引擎如何(例如 InnoDB 或 MyISAM),这个“比较和交换”语句是否总是原子的?:

UPDATE tbl_name SET locked=1 WHERE id=ID AND locked <> 1;

我问这个是因为我打算使用该语句来执行与事务和非事务数据库表兼容的伪行级锁定。

这是推荐用于 MyISAM的方法,但我不确定这是否适用于 InnoDB,因为文档建议改用事务。

4

1 回答 1

4

Yes. In InnoDB, the row will be locked (make you have an unique index on id, the update locks all rows it has to scan), updated and the lock released. If you are not in an explicit transaction / auto-commit is on, each statement is run in its own transaction, but it's still a transaction and lockings are performed

于 2012-08-13T17:59:43.817 回答