SQL死锁

news/2024/11/23 16:58:30/

前言:

使用脚本刷数据时,开多线程经常遇到死锁现象,面试也经常问到,故开此篇

日志错误示例:

### Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
### The error may exist in com/patpat/product/mapper/SaleProductSkuMapper.java (best guess)
### The error may involve com.patpat.product.mapper.SaleProductSkuMapper.updateById-Inline
### The error occurred while setting parameters
### SQL: UPDATE ps_sale_product_sku_magic_t_grobal_30002  SET sku_id=?, product_id=?,    sku_code=?, image=?  WHERE id=?
### Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transactionat org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:271)at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(Abs

分析:

死锁产生的原因:

①不可剥夺(只能由占有资源的进程释放)

②循环等待(每个进程在等待其他进程释放,而其他进程也在等待)

③互斥条件 (一个资源只能同时被一个进程使用)

④请求与保持(申请新资源,并还不释放已占有的)

sql死锁模拟:

当多个事务同时访问数据库中的数据并且存在竞争条件时,就有可能发生死锁。下面是一个示例,展示了一个可能导致死锁的情况:

假设有两个用户,用户A和用户B,同时执行以下两个事务:

事务1(用户A):

BEGIN TRANSACTION;
UPDATE orders SET status = 'processing' WHERE order_id = 1;
UPDATE products SET stock = stock - 1 WHERE product_id = 1;
COMMIT;

事务2(用户B):

BEGIN TRANSACTION;
UPDATE products SET stock = stock - 1 WHERE product_id = 1;
UPDATE orders SET status = 'processing' WHERE order_id = 1;
COMMIT;

在以上操作中,存在:用户A执行自己的第一条sql,用户B执行自己第一条sql。用户A再准备执行自己的第二条时,却不能执行了,用户B也不能再执行,于是陷入死锁。如下图:

 死锁产生!

解决办法: 

①大事务拆成小事务,尽可能缩小事务范围

大事务:将多个操作放在一个事务中执行

优点:这样可以减少事务的提交和回滚次数,提高性能。

缺点:如果事务过大,涉及到的数据量多,会增加事务持有锁的时间,增加死锁的风险。

小事务:将多个操作分成多个小事务,

优点:每个小事务只涉及少量的数据,尽快释放锁资源。这样可以减少事务持有锁的时间,降低死锁的风险

 ②业务中存在更新前和更新后一模一样的不再执行更新

场景:通过运维拉去mysql日志,查询发现,下面2事务,对表ps_option_value666进行更新操作,查询数据库发现,要更新的数据和想更新的数据,一模一样!

解决方案:在代码层面检查对象内容如果一样,就不更新了!

数据库死锁日志中部分日志:

数据库原有数据:

 数据库死锁日志:

2023-06-15 01:35:18 0x2b170f8877002023-06-15 01:35:18 0x2b170f887700
*** (1) TRANSACTION:
TRANSACTION 15159595386, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1136, 5 row lock(s)
MySQL thread id 506546, OS thread handle 47376231835392, query id 4253348818 172.31.19.109 app_product_0 updating
UPDATE ps_option_value666  SET option_id=6,
value='18-24 Months'  WHERE id=1118
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1236 page no 10 n bits 0 index PRIMARY of table `product_service`.`ps_option_value666` trx id 15159595386 lock_mode X locks rec but not gap waiting
Record lock, heap no 224 PHYSICAL RECORD: n_fields 11; compact format; info bits 00: len=4; bufptr=0x2b231cfcf469; hex= 0000045e; asc    ^;;1: len=6; bufptr=0x2b231cfcf46d; hex= 00000027a645; asc    ' E;;2: len=7; bufptr=0x2b231cfcf473; hex= 940000030237da; asc      7 ;;3: len=4; bufptr=0x2b231cfcf47a; hex= 00000000; asc     ;;4: len=4; bufptr=0x2b231cfcf47e; hex= 00000006; asc     ;;5: len=12; bufptr=0x2b231cfcf482; hex= 31382d3234204d6f6e746873; asc 18-24 Months;;6: SQL NULL;7: len=8; bufptr=0x2b231cfcf48e; hex= 0000000000001840; asc        @;;8: len=4; bufptr=0x2b231cfcf496; hex= 5b061db0; asc [   ;;9: len=4; bufptr=0x2b231cfcf49a; hex= 5f6df00d; asc _m  ;;10: len=0; bufptr=0x2b231cfcf49e; hex= ; asc ;;*** (2) TRANSACTION:
TRANSACTION 15159595381, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1136, 6 row lock(s)
MySQL thread id 506549, OS thread handle 47373393610496, query id 4253348825 172.31.45.125 app_product_0 updating
UPDATE ps_option_value666  SET option_id=6,
value='4-5 Years'  WHERE id=1446
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 1236 page no 10 n bits 0 index PRIMARY of table `product_service`.`ps_option_value666` trx id 15159595381 lock_mode X locks rec but not gap
Record lock, heap no 226 PHYSICAL RECORD: n_fields 11; compact format; info bits 00: len=4; bufptr=0x2b231cfcf4e0; hex= 00000460; asc    `;;1: len=6; bufptr=0x2b231cfcf4e4; hex= 00000027a645; asc    ' E;;2: len=7; bufptr=0x2b231cfcf4ea; hex= 940000030237f6; asc      7 ;;3: len=4; bufptr=0x2b231cfcf4f1; hex= 00000000; asc     ;;4: len=4; bufptr=0x2b231cfcf4f5; hex= 00000006; asc     ;;5: len=9; bufptr=0x2b231cfcf4f9; hex= 352d36205965617273; asc 5-6 Years;;6: SQL NULL;7: len=8; bufptr=0x2b231cfcf502; hex= 0000000000002440; asc       $@;;8: len=4; bufptr=0x2b231cfcf50a; hex= 5b061dbf; asc [   ;;9: len=4; bufptr=0x2b231cfcf50e; hex= 5f6df00d; asc _m  ;;10: len=0; bufptr=0x2b231cfcf512; hex= ; asc ;;[bitmap of 256 bytes in hex: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ]
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1236 page no 12 n bits 0 index PRIMARY of table `product_service`.`ps_option_value666` trx id 15159595381 lock_mode X locks rec but not gap waiting
Record lock, heap no 66 PHYSICAL RECORD: n_fields 11; compact format; info bits 00: len=4; bufptr=0x2b231cf95154; hex= 000005a6; asc     ;;1: len=6; bufptr=0x2b231cf95158; hex= 00000027a645; asc    ' E;;2: len=7; bufptr=0x2b231cf9515e; hex= 940000030a0a02; asc        ;;3: len=4; bufptr=0x2b231cf95165; hex= 00000000; asc     ;;4: len=4; bufptr=0x2b231cf95169; hex= 00000006; asc     ;;5: len=9; bufptr=0x2b231cf9516d; hex= 342d35205965617273; asc 4-5 Years;;6: SQL NULL;7: len=8; bufptr=0x2b231cf95176; hex= 0000000000002240; asc       "@;;8: len=4; bufptr=0x2b231cf9517e; hex= 5b061fd6; asc [   ;;9: len=4; bufptr=0x2b231cf95182; hex= 5f6df00d; asc _m  ;;10: len=0; bufptr=0x2b231cf95186; hex= ; asc ;;*** WE ROLL BACK TRANSACTION (2)


http://www.ppmy.cn/news/404163.html

相关文章

python 加速(1)

文章目录 简单步骤像Python一样做torch 的一切安装Cmake安装 Torch (GPU)CMakeLists.txt试用小样设置 CLion 环境 Cuda配置VS C 环境建上手的文件step1: interpolation.cppstep2: interpolation_kernel.custep3: include/ utils.hstep4: setup.pystep5: …

虚拟机使用

文章目录 VMWare安装Warning 系统安装MacOS解锁工具Unlocker Warning VMWare配置系统启动安装VMware tools联网 VMWare安装 Warning 安装完VMWare后要在BIOS中开启intel VT(虚拟化),否则安装过程中会出错,提示“Intel VT-x处于禁…

Linux下的打包和压缩/解压解包

文章目录 一、打包和压缩二、Linux下进行打包和压缩1.zip指令,unzip指令2.tar指令 一、打包和压缩 打包呢就是把所有东西装在一起,然后压缩就是将这一包东西给它合理摆放,腾出更多的空间,以便放更多的东西。 压缩可以将如果东西是…

基于html+css的图展示127

准备项目 项目开发工具 Visual Studio Code 1.44.2 版本: 1.44.2 提交: ff915844119ce9485abfe8aa9076ec76b5300ddd 日期: 2020-04-16T16:36:23.138Z Electron: 7.1.11 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Windows_NT x64 10.0.19044 项目…

【Linux】基础IO——文件描述符:缓冲区的理解

上个月学校考试,进行课程复习,一直没有更新博客,现考试结束,继续保持更新,欢迎大家关注! 目录 1 模仿C库自主封装简单的文件接口2 对缓冲区的理解2.1 数据刷新到磁盘的过程分析2.2 如何强制刷新内核 1 模仿…

【RH850/U2A】:Task激活过程

Task激活过程 Autostart非AutostartTASK(Default_Init_Task)EcuM_StartupTwo(void)SchM_Init(void)BswM_Init(xx)Rte_Start(void)Task激活过程,分自动运行(AutoStart)和非自动运行。 Autostart 在DavinciCfg中的配置如下: Autostart意味作在执行StartOS()后就开始运行了。…

任务4

1朴素贝叶斯 朴素贝叶斯的原理 利用朴素贝叶斯模型进行文本分类 朴素贝叶斯1 2SVM模型 SVM的原理 利用SVM模型进行文本分类 3LDA主题模型 pLSA、共轭先验分布 LDA 使用LDA生成主题特征,在之前特征的基础上加入主题特征进行文本分类 LDA数学八卦 lda2 合并特征 1&a…

04任务列表

任务列表 引导学生重新审视笔记的价值 讲解笔记的正确定位 讲解优秀笔记的标准 笔记工具的特征及选择 - [ ] 青菜 注意: -、[ 、] 后,都一定要有空格!无快捷键,通过鼠标操作。 菜单栏:选中文字—>段落—>任务…