mysql数据库死锁问题排查过程 | 数据库论坛-大发黄金版app下载

(这个问题我排查了很久,期间也找很多dba帮忙排查过,但是最终还是靠自己断断续续排查了一个月的时间才定位到,问题还是挺有意思的,大家可以看看。如果看不懂我觉得也没关系,因为他牵扯到的内容比较多,需要一些背景知识,后面再回过头来看就好了)
现象
某天晚上,同事正在发布,突然线上大量报警,很多是关于数据库死锁的,报警提示信息如下:

{"errorcode":"system_error","errormsg":"nested exception is org.apache.ibatis.exceptions.persistenceexception:
error updating database. cause: err-code: [tddl-4614][err_execute_on_mysql]
deadlock found when trying to get lock;
the error occurred while setting parameters\n### sql:
update fund_transfer_stream set gmt_modified=now(),state = ? where fund_transfer_order_no = ? and seller_id = ? and state = 'new'

通过报警,我们基本可以定位到发生死锁的数据库以及数据库表。先来介绍下本文案例中涉及到的数据库相关信息。
背景情况
我们使用的数据库是mysql 5.7,引擎是innodb,事务隔离级别是read-commited。
数据库版本查询方法:

select version();

引擎查询方法:

show create table fund_transfer_stream;

建表语句中会显示存储引擎信息,形如:engine=innodb
事务隔离级别查询方法:

select @@transaction_isolation

事务隔离级别设置方法(只对当前session生效):
ps:注意,如果数据库是分库的,以上几条sql语句需要在单库上执行,不要在逻辑库执行。
发生死锁的表结构及索引情况(隐去了部分无关字段和索引):

create table `fund_transfer_stream` ( 
  `id` bigint(20) unsigned not null auto_increment comment '主键',
  `gmt_create` datetime not null comment '创建时间',
  `gmt_modified` datetime not null comment '修改时间', 
  `pay_scene_name` varchar(256) not null comment '支付场景名称', 
  `pay_scene_version` varchar(256) default null comment '支付场景版本',
  `identifier` varchar(256) not null comment '唯一性标识',
  `seller_id` varchar(64) not null comment '卖家id',
  `state` varchar(64) default null comment '状态', `fund_transfer_order_no` varchar(256) 
  default null comment '资金平台返回的状态', 
  primary key (`id`),
  key `idx_seller` (`seller_id`),
  key `idx_seller_transno` (`seller_id`,`fund_transfer_order_no`(20))
  ) engine=innodb default charset=utf8mb4 comment='资金流水';

该数据库共有三个索引,1个聚簇索引(主键索引),2个非聚簇索(非主键索引)引。
聚簇索引:

primary key (`id`)

非聚簇索引:

key `idx_seller` (`seller_id`),
key `idx_seller_transno` (`seller_id`,`fund_transfer_order_no`(20))

以上两个索引,其实idx_seller_transno已经覆盖到了idx_seller,由于历史原因,因为该表以seller_id分表,所以是先有的idx_seller,后有的idx_seller_transno
死锁日志
当数据库发生死锁时,可以通过以下命令获取死锁日志:

show engine innodb status

发生死锁,第一时间查看死锁日志,得到死锁日志内容如下:
简单解读一下死锁日志,可以得到以下信息:

transactions deadlock detected, dumping detailed information.
2019-03-19t21:44:23.516263 08:00 5877341 [note] innodb: 
*** (1) transaction:
transaction 173268495, active 0 sec fetching rows
mysql tables in use 1, locked 1
lock wait 304 lock struct(s), heap size 41168, 6 row lock(s), undo log entries 1
mysql thread id 5877358, os thread handle 47356539049728, query id 557970181 11.183.244.150 fin_instant_app updating
update `fund_transfer_stream` set `gmt_modified` = now(), `state` = 'processing' where ((`state` = 'new') and (`seller_id` = '38921111') and (`fund_transfer_order_no` = '99010015000805619031958363857'))
2019-03-19t21:44:23.516321 08:00 5877341 [note] innodb: 
*** (1) holds the lock(s):
record locks space id 173 page no 13726 n bits 248 index idx_seller_transno of table `xxx`.`fund_transfer_stream` trx id 173268495 lock_mode x locks rec but not gap
record lock, heap no 168 physical record: n_fields 3; compact format; info bits 0
2019-03-19t21:44:23.516565 08:00 5877341 [note] innodb: 
*** (1) waiting for this lock to be granted:
record locks space id 173 page no 12416 n bits 128 index primary of table `xxx`.`fund_transfer_stream` trx id 173268495 lock_mode x locks rec but not gap waiting
record lock, heap no 56 physical record: n_fields 17; compact format; info bits 0
2019-03-19t21:44:23.517793 08:00 5877341 [note] innodb: 
*** (2) transaction:
transaction 173268500, active 0 sec fetching rows, thread declared inside innodb 81
mysql tables in use 1, locked 1
302 lock struct(s), heap size 41168, 2 row lock(s), undo log entries 1
mysql thread id 5877341, os thread handle 47362313119488, query id 557970189 11.131.81.107 fin_instant_app updating
update `fund_transfer_stream_0056` set `gmt_modified` = now(), `state` = 'processing' where ((`state` = 'new') and (`seller_id` = '38921111') and (`fund_transfer_order_no` = '99010015000805619031957477256'))
2019-03-19t21:44:23.517855 08:00 5877341 [note] innodb: 
*** (2) holds the lock(s):
record locks space id 173 page no 12416 n bits 128 index primary of table `fin_instant_0003`.`fund_transfer_stream_0056` trx id 173268500 lock_mode x locks rec but not gap
record lock, heap no 56 physical record: n_fields 17; compact format; info bits 0
2019-03-19t21:44:23.519053 08:00 5877341 [note] innodb: 
*** (2) waiting for this lock to be granted:
record locks space id 173 page no 13726 n bits 248 index idx_seller_transno of table `fin_instant_0003`.`fund_transfer_stream_0056` trx id 173268500 lock_mode x locks rec but not gap waiting
record lock, heap no 168 physical record: n_fields 3; compact format; info bits 0
2019-03-19t21:44:23.519297 08:00 5877341 [note] innodb: *** we roll back transaction (2)

1、导致死锁的两条sql语句分别是:

2、事务1,持有索引idx_seller_transno的锁,在等待获取primary的锁。
3、事务2,持有primary的锁,在等待获取idx_seller_transno的锁。
4、因事务1和事务2之间发生循环等待,故发生死锁。
5、事务1和事务2当前持有的锁均为:lock_mode x locks rec but not gap

  • 所有update都通过主键id进行。
    所以,根本解决办法就是改代码:
    所有update都通过主键id进行。
    在同一个事务中,避免出现多条update语句修改同一条记录。
本作品采用《cc 协议》,转载必须注明作者和本文链接
missyou-coding
讨论数量: 1

所以我会这样来编写代码:

。。。。。。
 db::begintransaction();
 $record =  userdirectrewardmodel::query()
            ->where('level', $level)
            ->where('is_receive', 0)
            ->first();
 $newrecord = userdirectrewardmodel::lockforupdate()->find( $record->id );
。。。。。。 
4个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图