当前位置:网站首页>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;
边栏推荐
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
- 2022-08-01 安装mysql监控工具phhMyAdmin
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- MySQL优化策略
- Can Youxuan database import wrongly be restored?
- 乱七八糟的网站
- Hiring a WordPress Developer: 4 Practical Ways
- How to adjust the cross cursor too small, CAD dream drawing calculation skills
- Fly propeller power space future PIE - Engine Engine build earth science
- Effects of Scraping and Aggregation
猜你喜欢

Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

BI-SQL丨WHILE

nacos启动报错,已配置数据库,单机启动

Check if IP or port is blocked

volatile原理解析

Chengdu openGauss user group recruit!

FOFAHUB使用测试

Unable to log in to the Westward Journey

MySQL8 下载、启动、配置、验证
随机推荐
2022-08-01 反思
求大神解答,这种 sql 应该怎么写?
项目后台技术Express
swift项目,sqlcipher3 -> 4,无法打开旧版数据库有办法解决吗
AntPathMatcher uses
Hash collisions and consistent hashing
AOF rewrite
Redis Subscription and Redis Stream
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
to-be-read list
How to adjust the cross cursor too small, CAD dream drawing calculation skills
MySQL optimization strategy
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
使用docker安装mysql
AI目标分割能力,无需绿幕即可实现快速视频抠图
【 wheeled odometer 】
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
Personal blog system project test