数据库主键更新死锁问题

记一次压测数据死锁问题:并发场景为秒杀减库存的场景,使用Jmeter并发调用秒杀接口,秒杀使用数据库乐观锁,主键更新,正常来说不会有死锁问题。分析框架的日志肯定不能找出什么问题,最后再mysql的binlog里面发现了死锁的原因,Deep or long search in the lock table wait-for graph we will rollback transaction。经过官网文档解释:当事务等待超过200 (参数LOCK_MAX_DEPTH_IN_DEADLOCK_CHECK默认值200),mysql会认为发生了死锁,从而回滚事务。以下截图为官网说明:


web框架日志:

ERROR 06-27 17:46:19(Log4jFilter.java:152): {conn-10236, pstmt-21039} execute error. UPDATE coupon_pack_share cps SET cps.receiveCount = cps.receiveCount + 1 WHERE cps.shareId = ? AND cps.receiveCount + 1<= cps.limitCount

com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction

        at sun.reflect.GeneratedConstructorAccessor53.newInstance(Unknown

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

        at com.mysql.jdbc.Util.getInstance(Util.java:386)

        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1065)

        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)

        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)

        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)

        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)

        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)

mysql 日志:


你可能感兴趣的:(数据库主键更新死锁问题)