- 请先稍微了解一下mysql Innodb引擎以及各个事务隔离级别下实现锁的sql语句;
- 失效的旧版本写法:
var balance model.Balances
err := tx.Table(model.BalancesTableName).Set("gorm:query_option", "FOR UPDATE").Where("uid = ?", userId).First(&balance).Error
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
r.log.Errorf("[data.TransferAccounts] get from_info error:%v", err)
}
return err
}
- 执行的sql语句:
select * from balances where id = xx
, 没有生成子句FOR UPDATE
db.Clauses(clause.Locking{Strength: "UPDATE"}).Find(&users)
// SELECT * FROM `users` FOR UPDATE
db.Clauses(clause.Locking{
Strength: "SHARE",
Table: clause.Table{Name: clause.CurrentTable},
}).Find(&users)
// SELECT * FROM `users` FOR SHARE OF `users`
新版本文档