MySQL Atomic UPDATE in InnoDB vs MyISAM

痴心易碎 提交于 2019-12-30 19:27:10

问题


Is this "compare and swap" statement always atomic regardless of engine (e.g. InnoDB or MyISAM)? :

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

I ask this because I intend to use this statement to do pseudo row-level locking that is compatible with both transactional and non-transactional database tables.

This is the method that is recommended for MyISAM, but I am uncertain as to whether this works for InnoDB since the documentation suggests using transactions instead.


回答1:


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



来源:https://stackoverflow.com/questions/11502135/mysql-atomic-update-in-innodb-vs-myisam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!