[MySQL] 死锁简析

出现死锁的原因

两个线程或者两个事务,都需要对方释放同一资源的锁,从而导致死锁。

解决策略

  1. 尽可能的将事务力度控制到最小
  2. 超时自动释放,通过SHOW VARIABLES WHERE Variable_name = 'innodb_lock_wait_timeout’语句可以查看引擎默认超时时间
  3. 开启死锁检测,通过SHOW VARIABLES WHERE Variable_name LIKE ‘%innodb_deadlock%’ 语句可以查看是否开启;如果发生死锁,回滚后面的事务。

死锁回滚条件

If the LATEST DETECTED DEADLOCK section of InnoDB Monitor output includes a message stating, “TOO DEEP OR LONG SEARCH IN THE LOCK TABLE WAITS-FOR GRAPH, WE WILL ROLL BACK FOLLOWING TRANSACTION,” this indicates that the number of transactions on the wait-for list has reached a limit of 200. A wait-for list that exceeds 200 transactions is treated as a deadlock and the transaction attempting to check the wait-for list is rolled back. The same error may also occur if the locking thread must look at more than 1,000,000 locks owned by transactions on the wait-for list.

查询数据库实例中事务相关信息

#查询进行中的事务:
SELECT * FROM information_schema.INNODB_TRX
#查询等待锁的事务
SELECT * FROM information_schema.INNODB_LOCK_WAITS
#查询获取锁的事务
SELECT * FROM information_schema.INNODB_LOCKS

参考:
https://www.cnblogs.com/sunss/p/7875012.html
https://www.cnblogs.com/lamp01/p/10751908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值