mysql死锁例子

一、mysql死锁

比如2个事务,事务A、事务B,都查询id=74和id=78的数据,并加锁

事务A,select * from t where id=74 for update,对id=74数据加锁

然后,事务B,select * from t where id=78 for update,对id=78数据加锁

事务A,此时又需要查询id=78的数据,于是select * from t where id=78 for update,因为事务B已经加过id=78的锁了,所以事务A就会一直等待

事务B,此时又需要查询id=74的数据,于是select * from t where id=74 for update,因为事务A已经加过id=74的锁了,所以事务B也会一直等待

此时就发生了死锁,会等待系统设置的超时时间,mysql会检查这种死锁,超过超时时间(mysql设置innodb_lock_wait_timeout),然后报错回滚,才打破死锁

1.1 navicat新建一个person表

1.2 开启事务B

1.3 开启事务B

1.4 回滚与提交

参考

猜你喜欢

转载自blog.csdn.net/qq_38826019/article/details/113625264