Mysql InnoDB 數據更新/刪除導致鎖表


一. 如下 對賬表 數據結構 

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'

 沒有索引時:

添加索引后:

 其中,rows表示MySQL根據表統計信息及索引選用情況,估算的找到所需的記錄所需要讀取的行數。可見,沒有索引時讀取了所有行(表里共33條記錄),而添加索引后只讀取特定的1行。


 

ref:https://www.cnblogs.com/zmduan/p/5033047.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM