当前位置:网站首页>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,user3
Manual 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 user1
The 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.
边栏推荐
猜你喜欢
随机推荐
【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
DBPack SQL Tracing 功能及数据加密功能详解
Solve the problem that MySQL cannot insert Chinese data
opencv实时人脸检测
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
面经汇总-社招-6年
Shell nl命令详解(显示行号、读取文件)
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution
浅谈游戏音效测试点
电商库存系统的防超卖和高并发扣减方案
Leetcode73. Matrix Zeroing
B001 - 基于STM32的智能生态鱼缸
LeaRun.net快速开发动态表单
SQL函数 TO_DATE(二)
极化微波成像概述
【Day_09 0427】 另类加法
Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?
opencv real-time face detection
打开微信客服
How to build a CMDB driven by consumption scenarios?