Mysql InnoDB 数据更新/删除导致锁表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Mysql InnoDB 数据更新/删除导致锁表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4601字,纯文字阅读大概需要7分钟。
内容图文
一. 如下对账表数据结构
create table t_cgw_ckjnl ( CNL_CODE varchar(10) default ' ' not null comment '通道编码', CNL_PLT_CD varchar(32) default ' ' not null comment '通道平台号', CNL_TYP varchar(10) default ' ' not null comment '通道类型', CHK_BAT_NO varchar(32) default ' ' not null comment '对账批次号', BAT_NO varchar(32) default ' ' not null comment '交易批次号', SEQ_NO varchar(8) default ' ' not null comment '批次序列号', CHK_ORD_NO varchar(64) default ' ' not null comment '对账订单号', CHK_TYP varchar(10) default ' ' not null comment '对账类型', CHK_MOD varchar(2) default ' ' not null comment '对账方式(预留:通道订单号、交易批次号+通道订单号、交易批次号+批次序列号)', CHK_DT varchar(8) default ' ' not null comment '对账日期', CHK_TM varchar(6) default ' ' not null comment '对账时间', CHK_STS varchar(1) default '0' not null comment '对账状态', REQ_DT varchar(8) default '0' not null comment '交易请求日期', IIF_TYP varchar(10) default '0' not null comment '接口类型', ORD_NO varchar(32) default '0' not null comment '交易订单号', CGW_STS varchar(2) default ' ' not null comment '交易状态', TXN_AMT decimal(18,2) not null comment '交易金额', FEE_AMT decimal(18,2) default '0.00' not null comment '手续费', BAT_FLG varchar(2) default ' ' not null comment '批量标识(B-批量,S-单笔)', FIELD varchar(64) null comment '备用字段', TM_SMP varchar(26) default ' ' not null comment '时间戳', NOD_ID varchar(32) null comment '交易来源', primary key (CHK_BAT_NO, CHK_ORD_NO) ) comment '对账流水临时表' engine=InnoDB ;
二. 现象
当两个对账交易同时发生时,因都对这个表执行如下delete操作,当2个delete语句同时发生时,产生死锁。
sql:
delete from T_CGW_CKJNL where chk_typ=#{chk_typ} and cnl_code=#{cnl_code} and cnl_plt_cd=#{cnl_plt_cd}
交易1异常:
INFO[11-02 13:58:01,697] -> update sql:[delete from T_CGW_CKJNL where chk_typ='SP' and cnl_code='EPCC' and cnl_plt_cd='Z2027533000016' ] INFO[11-02 13:58:01,767] -> 对账执行异常, java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy256.deleteCkJnl(Unknown Source) at com.murong.ecp.app.bpg.cgw.service.db.CgwCkJnlDBService.deleteCkJnl(CgwCkJnlDBService.java:29) at com.murong.ecp.app.bpg.cgw.service.biz.CheckFlowService.check(CheckFlowService.java:352) at com.murong.ecp.app.bpg.cgw.service.biz.CheckFlowService.checkExecute(CheckFlowService.java:116) at com.murong.ecp.app.bpg.cgw.action.cgwchkbpc1.CheckFlowAction.doProcess(CheckFlowAction.java:61) ... Nested Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) java.lang.reflect.Constructor.newInstance(Constructor.java:423) com.mysql.jdbc.Util.handleNewInstance(Util.java:377) com.mysql.jdbc.Util.getInstance(Util.java:360) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907) com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141) com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077) com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062) org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:97) org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:97) ... java.lang.Thread.run(Thread.java:748)
交易2异常:
INFO[11-02 13:58:01,697] -> update sql:[delete from T_CGW_CKJNL where chk_typ='Refund' and cnl_code='EPCC' and cnl_plt_cd='Z2027533000016' ] INFO[11-02 13:58:01,767] -> 对账执行异常, java.lang.reflect.UndeclaredThrowableException ... Nested Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction ...
三.解决办法
MySQL的InnoDB存储引擎支持行级锁,InnoDB的行锁是通过给索引项加锁实现的。这就意味着只有通过索引条件检索数据时,InnoDB才使用行锁,否则使用表锁。
根据上面的数据更新语句涉及到的字段chk_typ,cnl_code,cnl_plt_cd上都没有索引,所以并发时导致表被锁。
解决办法就是为字段chk_typ,cnl_code,cnl_plt_cd添加索引。
EXPLAIN delete from T_CGW_CKJNL where chk_typ='Pay' and cnl_plt_cd='Z20275330000161' and cnl_code='EPCC'
没有索引时:
添加索引后:
内容总结
以上是互联网集市为您收集整理的Mysql InnoDB 数据更新/删除导致锁表全部内容,希望文章能够帮你解决Mysql InnoDB 数据更新/删除导致锁表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。