笔者最近在生产环境错误日志上看到updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction 这样的日志 ,网上看了很多文章 发现这篇文章 跟自己的场景非常接近。也没像作者翻binlog日志
但综合分析我的情况应该是相同表记录行锁冲突
在我一次执行三个update table1 ,table2 set table.xx=table2.xx where ..
update table1,table2 set table1.yy=table2.yy where ...
update table1,table2 set table1.xx=table2.xx where ...
update table1,table2 set table1.zz=table2.zz where ...
而且我的where 条件列是没有建立索引 所以锁住的是整表。
事务一直执行过程中 锁住记录后没有释放 事务2执行时 遇到相同记录 等待事务1 事务三 又等待事务二 因为有相同行数涉及 ,这个时候 事务1执行完毕 等待事务3 就造成死锁现象
当事务1执行语句1时 锁住了某几行 这时事务2执行的时候 也需要更新相同的几行 但发现那几行被事务1锁住
上图是我接收到的错误报警,SQLSTATE[4