当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
快速抽取resnet_v2_152中间的特征层
突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
浅谈大数据背景下数据库安全保障体系
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
Review实战经典:2 种封装风格,你偏爱哪种?
极化微波成像概述3
typora操作手册
Leetcode75. 颜色分类
解决MySQL插入不了中文数据问题
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
JVM运行时数据区与JMM内存模型是什么
opencv语法Mat类型总结
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
GTK修改pixmap像素,提取pixmap像素RGB值
AntDB数据库亮相24届高速展,助力智慧高速创新应用
【无标题】setInterval和setTimeout详解
QT常用全局宏定义
【Day_09 0427】 另类加法
B002 - Embedded Elderly Positioning Tracking Monitor









