查询mysql数据库事物锁

1:查看当前的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;

2:查看当前锁定的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;

3:查看当前等锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;

查出死锁进程:SHOW PROCESSLIST
杀掉进程 KILL 420821;

测试:
start TRANSACTION;
select * from library_books where tag=’e0040150653e6e64’ for update;

再开一个update:
update library_books set book_source=1 WHERE tag=’e0040150653e6e64’;
会报错:Lock wait timeout exceeded; try restarting transaction

查询结果SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX:
6452411 RUNNING 2018-08-09 14:58:02 3 150640 0 1 3 1136 2 0 0 REPEATABLE READ 1 1 0 0 0 0
查询结果SHOW PROCESSLIST:
150640 test bookplatform_test Sleep 86
查询结果SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS:无
查询结果SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS:无

反驳mysql的for update行级锁只能锁查询id的情况,有些程序员为了使用行级锁将sql这么写:
SELECT * FROM account WHERE id=
(select id from account where user_id = #{userId} and user_type = #{userType.index}) for update。这样是没有必要的,测试mysql数据库版本:select version()—->5.7.22-log

你可能感兴趣的:(mysql)