試試這個。打開兩個終端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 ...進行更新。選擇更新塊另一個選擇更新或在同一記錄上選擇鎖定共享模式。 –