簡體   English   中英

Mysql innodb 行鎖不起作用

[英]Mysql innodb row lock doesn't work

我正在使用 mysql v5.6 。

當我使用 php 發送以下 mysql 查詢時,它將選擇一些同時鎖定它們的行:

SELECT * FROM accounts WHERE id = 1 FOR UPDATE;

我收到以下錯誤消息:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your Mysql server version for the right syntax to use near 'LIMIT 0, 25' at line 2 

謝謝

InnoDB 實現了標准的row-level locking ,其中有兩種類型的鎖, shared (S) locksexclusive (X) locks

A shared (S) lock permits a transaction to read a row. An exclusive (X) lock permits a transaction to update or delete a row.

使用SELECT FOR UPDATE鎖定更新行僅在禁用autocommit時適用(通過使用START TRANSACTION或將autocommit設置為0 。如果啟用自動提交,則不會鎖定與規范匹配的行。

參考鏈接

https://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

https://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html

Mysql innodb 行鎖不起作用

現在,我認為它的問題是你不應該使用transaction not startedtransaction not started ,只會產生很大的不同,所以你應該像我的代碼一樣START TRANSACTION ,並且

有時,在一個正在運行的transaction ,兩個相同的語句獲得不同的值時會發生這種情況,因為其他一些事務修改了表的行。

喜歡你的如下:

transaction1> START TRANSACTION;
transaction1> SELECT * FROM accounts WHERE id=1 FOR UPDATE;

例如:

transaction1> SELECT first_name, last_name FROM customer WHERE id = 3 FOR UPDATE;

+------------+-----------+
| first_name | last_name |
+------------+-----------+
| JMAIL      | KRISH     |
+------------+-----------+

1 row in set (0.00 sec)

你應該參考這個鏈接:

http://www.mysqlperformanceblog.com/2012/03/27/innodbs-gap-locks/

http://dev.mysql.com/doc/refman/5.1/en/innodb-locks-set.html

https://stackoverflow.com/a/22007412/3242978

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM