当前位置:网站首页>Go GORM transaction instance analysis
Go GORM transaction instance analysis
2022-08-01 18:09:00 【Yisuyun】
Go GORM transaction instance analysis
This article mainly introduces the relevant knowledge of "Go GORM transaction instance analysis". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this "Go GORM transaction instance analysis"Transaction Case Analysis" article can help you solve the problem.
Disable default transactions
To ensure data consistency, GORM performs write operations (create, update, delete) within a transaction.If this is not required, you can disable it on initialization, which will give about 30%+ performance improvement.
// Disable db globally, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ SkipDefaultTransaction: true,})// Persistent session modetx := db.Session(&Session{SkipDefaultTransaction: true})tx.First(&user, 1)tx.Find(&users)tx.Model(&user).Update("Age", 18)Transactions
To perform a series of operations in a transaction, the general flow is as follows:
db.Transaction(func(tx *gorm.DB) error { // Do some db operations in a transaction (from here you should use 'tx' instead of 'db') if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { // Returning any error will rollback the transaction return err } if err := tx.Create(&Animal{Name:"Lion"}).Error; err != nil { return err } } // return nil Commit transaction return nil})Nested Transactions
GORM supports nested transactions, where you can roll back part of an operation performed within a larger transaction, example:
db.Transaction(func(tx *gorm.DB) error { tx.Create(&user1) tx.Transaction(func(tx2 *gorm.DB) error { tx2.Create(&user2) return errors.New("rollback user2") // Rollback user2 }) tx.Transaction(func(tx2 *gorm.DB) error { tx2.Create(&user3) return nil }) return nil})// Commit user1,user3Manual transactions
// Begin transaction tx := db.Begin()// Do some db operations in transaction (from here you should use 'tx' instead of 'db') tx.Create(...)// ...// Rollback transaction tx.Rollback()// Otherwise, commit transaction tx.Commit()
A special example
func CreateAnimals(db *gorm.DB) error { // Again, once the transaction starts, you should use tx to process the data tx := db.Begin() defer func() { if r := recover(); r != nil { tx.Rollback() } } }() if err := tx.Error; err != nil { return err } if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { tx.Rollback() return err } if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil { tx.Rollback() Return err } return tx.Commit().Error}SavePoint, RollbackTo
GORM provides SavePoint, Rollbackto to provide savepoint and rollback to savepoint, for example:
tx := db.Begin()tx.Create(&user1)tx.SavePoint("sp1")tx.Create(&user2)tx.RollbackTo("sp1") // Rollbackuser2tx.Commit() // Commit user1The content of "Go GORM transaction instance analysis" is introduced here, thank you for reading.If you want to know more industry-related knowledge, you can pay attention to the Yisuyun industry information channel. The editor will update different knowledge points for you every day.
边栏推荐
猜你喜欢
随机推荐
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
浅谈游戏音效测试点
Go iota关键字与枚举类型实现原理是什么
opencv如何实现图像倾斜校正
How opencv implements image skew correction
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
What is the JVM runtime data area and the JMM memory model
B001 - 基于STM32的智能生态鱼缸
后台管理系统的权限思路
【100个网络运维工作者必须知道的小知识!】
tooltip 控件
顺序表的简单描述及代码的简单实现
typora操作手册
极化微波成像概述3
阿里云的域名和ip绑定
What is the implementation principle of Go iota keyword and enumeration type
频域分析实践介绍
Golang协程调度器scheduler怎么使用
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
Solve the problem that MySQL cannot insert Chinese data









