当前位置:网站首页>Remember a gorm transaction and debug to solve mysql deadlock
Remember a gorm transaction and debug to solve mysql deadlock
2022-08-02 02:15:00 【Not the old king who lives next door】
前言
之前使用mysql和postgresql是通过sqlc包来使用,这一次使用gorm,还是遇到了很多问题,在此记录以下.
版本问题
gormAfter a major version update,The introduction and actual usage of the driver will be slightly different from the old version,Especially business、Use of locks and modification of configuration items.
新版本gormDatabase linking and package names have changed compared to the old version,如下:
老版本:
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
db, err := gorm.Open("mysql", "root:[email protected](127.0.0.1:3306)/db01?&parseTime=True&loc=Local")
而新版本:
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
})
The configuration items of the old version are passeddb.xx的形式修改:
db.LogMode(true)
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
db.SingularTable(true)
The new version passed&gorm.Config{}字段进行修改:
db, err = gorm.Open(mysql.Open(dbLink), &gorm.Config{
SkipDefaultTransaction: true, //开启事务
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
forupdateusage changed:
At first, it was still used in the old version,但是一直不成功,然后使用debug打印sql语句,发现生成的sql语句并没有for update相关操作,Go to find information,Found that the old version of the writing does not take effect,Replace with new spelling.
老版本:
result := tx.Debug().Set("gorm:query_option", "FOR UPDATE").First(&user1, arg.FromUserID)
新版本:
result = tx.Debug().Clauses(clause.Locking{
Strength: "UPDATE"}).First(&user2, arg.ToUserID)
Database deadlock troubleshooting:
In a scenario similar to a transfer,In order to ensure the consistency of transfers,Transactions are used in related interfaces to ensure successful transfers,In the last test someone transferred out of the same account,A deadlock occurs when someone transfers in;
According to online queries and a blog post by a big guy(http://t.csdn.cn/nAzEf)的思路下,Restored deadlock scenarios in the terminal.
分别开启两个事务,按照顺序进行update操作:在执行第4step is business2when the second operation in ,出现死锁.
事务1:
1 update user set collection_coins = collection_coins - 10 where id = 1537973755;
3 update user set collection_coins = collection_coins + 10 where id = 1338725819; 等待获取锁
事务2:
2 update user set collection_coins = collection_coins - 10 where id = 1338725819;
4 update user set collection_coins = collection_coins + 10 where id = 1537973755;
Query the table for related locks,Can see the previous step where the deadlock occurred:
Run the following two statements separately:
select * from information_schema.innodb_locks;
select * from information_schema.innodb_lock_waits;
运行:
SELECT * FROM information_schema.innodb_trx;
可以看到
可以看到,Both transactions acquired the lock,进行了第3个操作时,事务1阻塞,等待事务2释放
锁;如果此时事务2不释放锁,Instead, execute No4个操作,will result in a transaction2请求锁,But this time the lock is in the transaction1处,而事务1已经阻塞,无法释放锁,则事务2也会被阻塞,形成死锁.
The solution is:
调换4order of execution of the statements;
如果按照1-4-3-2order will not cause deadlock.
因为在执行1后,事务1获得锁1,此时事务2执行4,事务2请求锁1,此时事务2阻塞;
事务1再执行3,获得锁2;此时事务1执行完毕,commit释放锁1和锁2,事务2获得锁1,语句4执行成功,再执行语句2成功,则不会发生死锁.
Switching to the code is to arrange the order of execution,利用id为数字格式,可以比大小,You can arrange them by size;
边栏推荐
- leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
- leetcode / anagram in string - some permutation of s1 string is a substring of s2
- Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
- 工程师如何对待开源
- FOFAHUB使用测试
- 优炫数据库导库导错了能恢复吗?
- "NetEase Internship" Weekly Diary (3)
- Redis Persistence - RDB and AOF
- 2022-08-01 反思
- 密码学的基础:X.690和对应的BER CER DER编码
猜你喜欢
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
"NetEase Internship" Weekly Diary (1)
Electronic Manufacturing Warehouse Barcode Management System Solution
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
2022-08-01 mysql/stoonedb慢SQL-Q18分析
Service discovery of kubernetes
Redis 订阅与 Redis Stream
2023年起,这些地区软考成绩低于45分也能拿证
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
随机推荐
Ask God to answer, how should this kind of sql be written?
Coding Experience Talk
2022-08-01 安装mysql监控工具phhMyAdmin
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
记一次gorm事务及调试解决mysql死锁
AI target segmentation capability for fast video cutout without green screen
FOFAHUB使用测试
Outsourcing worked for three years, it was abolished...
Centos7 install postgresql and enable remote access
MySQL - CRUD operations
2022-08-01 Reflection
Analysis of volatile principle
nacos startup error, the database has been configured, stand-alone startup
oracle查询扫描全表和走索引
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
2023年起,这些地区软考成绩低于45分也能拿证
密码学的基础:X.690和对应的BER CER DER编码
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
LeetCode刷题日记:LCP 03.机器人大冒险
NIO‘s Sword(牛客多校赛)