外键和锁(二)

2014-11-24 16:02:15 · 作者: · 浏览: 1
InnoDB通常不会回滚大部分错误异常,但是除了死锁,发生死锁,InnoDB会立马回滚事务,这点需要注意
[plain] 
会话A:  
mysql> begin;  
Query OK, 0 rows affected (0.00 sec)  
  
mysql> select * from t where id=1 for update;  
+----+  
| id |  
+----+  
|  1 |  
+----+  
1 row in set (0.00 sec)  
  
会话B:  
mysql> begin;  
Query OK, 0 rows affected (0.00 sec)  
  
mysql> select * from t where id=2 for update;  
+----+  
| id |  
+----+  
|  2 |  
+----+  
1 row in set (0.00 sec)  
  
会话A:  
mysql>
select * from t where id=2 for update; --被阻塞 会话B: mysql> select * from t where id=1 for update; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction 会话A: +----+ | id | +----+ | 2 | +----+ 1 row in set (16.69 sec)