本文主要是介绍查询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数据库事物锁的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!