美文网首页
for update 和 lock in share mode

for update 和 lock in share mode

作者: 剑客kb | 来源:发表于2019-04-01 08:37 被阅读0次

共同点:两者都必须在事务中使用
不同点:for update 对记录加写锁,此时记录不能被其他线程加读锁或者写锁。
lock in share mode对记录加读锁,此时记录能被其他线程加读锁,不能加写锁。但是可以在当前事务内,再加写锁。


mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test where id=7 lock in share mode;
+----+----------+-------------+----------+------+----------+
| id | testEnum | testdecimal | sizetest | date | dateCopy |
+----+----------+-------------+----------+------+----------+
|  7 |        2 |        NULL |     NULL | NULL | NULL     |
+----+----------+-------------+----------+------+----------+
1 row in set (0.00 sec)

mysql> select * from test where id=7 for update;
+----+----------+-------------+----------+------+----------+
| id | testEnum | testdecimal | sizetest | date | dateCopy |
+----+----------+-------------+----------+------+----------+
|  7 |        2 |        NULL |     NULL | NULL | NULL     |
+----+----------+-------------+----------+------+----------+
1 row in set (0.00 sec)

相关文章

网友评论

      本文标题:for update 和 lock in share mode

      本文链接:https://www.haomeiwen.com/subject/zvswmqtx.html