试试这个。打开两个终端PowerShell的Windows中,xterm中,在Linux控制台,....连接到MySQL:
创建表child_codes(从MySQL documentation拍摄)
mysql> create table child_codes (counter_field integer);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into child_codes set counter_field = 1;
Query OK, 1 row affected (0.00 sec)
session 1 (terminal 1): session 2 (terminal 2):
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select counter_field from child_codes
lock in share mode;
+---------------+
| counter_field |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select counter_field from
child_codes lock in share mode;
+---------------+
| counter_field |
+---------------+
| 1 |
+---------------+
mysql> update child_codes set counter_field = 2;
ERROR 1205 (HY000): Lock wait timeout exceeded;
try restarting transaction
我也认为,如果其它事务执行查询共享锁选择模式下查询被阻塞(等待其他事务提交或回滚)。但是像达哈泽提到的如果该行没有被修改,它不会等待。我相信这个行为必须在MySQL文档中提及。
所以选择更新等待,如果bloked行修改或未由user1修改之前提交并锁定在共享模式等待只有阻止的行由user1修改之前提交? – user677900
区别在于,您可以在共享模式下使用select ... lock在2个事务中选择一个和相同的记录,但不能使用select ...进行更新。选择更新块另一个选择更新或在同一记录上选择锁定共享模式。 –