Mysql InnoDB 資料更新/刪除導致鎖表
阿新 • • 發佈:2018-11-07
一. 如下對賬表資料結構
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'
沒有索引時:
新增索引後:
ref:https://www.cnblogs.com/zmduan/p/5033047.html