当前位置:网站首页>Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
2022-07-30 22:17:00 【web18224617243】
学习笔记,写到哪是哪。
接着上一篇文章:Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)_的博客-CSDN博客
在项目中事务处理,回滚操作还是比较常见的需求。
本文测试一下gorm的事务处理效果。
项目地址:github地址
如果没有事务方面需求,也是可以全局禁用事务的,官方给出的性能提升是30%。
在student_service下新增TestTransaction方法。
方法代码如下:
//测试事务效果
func (t StudentImpl) TestTransaction() rsp.ResponseMsg {
log.Logger.Info("测试事务效果")
_db := mysql.GetDB()
_db.Transaction(func(tx *gorm.DB) error {
tx.Create(&db_entity.Student{
Name: "张飞", Age: 200,
})
var _student db_entity.Student
if _err := tx.Where("del_flag = 1").First(&_student).Error; _err != nil {
return _err
}
fmt.Println(_student)
return nil
})
return *rsp.SuccessMsg("测试成功")
}
简单说明
1、使用Transaction定义需要处理的事务方法,先创建一条数据,再构造一个异常。
2、如果异常了,按照事务处理方式,创建的数据也不会提交。
controller层增加接口代码,如下:
//测试事务效果
func (s StudentController) TestTransaction(context *gin.Context) {
log.Logger.Info("测试事务效果")
_rsp := services.StudentServ.TestTransaction()
context.JSON(http.StatusOK, _rsp)
}
测试效果



可以看出数据并没有插入到表中。
注意
感觉有个地方还是容易出错的,在Transaction定义事务方法的时候,方法内不要在使用db去处理,而是传参tx。不然并不会按照事务方式去处理。
小结
没啥好总结的。

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Detailed explanation of the delete problem of ClickHouse delete data
- Solve the problem of centos8 MySQL password ERROR 1820 (HY000) You must reset your password using the ALTER USER
- ClickHouse 数据插入、更新与删除操作 SQL
- Navicat new database
- cnpm installation steps
- 2022.7.27
- 正则表达式语法及使用
- language code table
- Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
- The most complete Redis basic + advanced project combat summary notes in history
猜你喜欢

ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c

MySQL Soul 16 Questions, How Many Questions Can You Last?

The mysql time field is set to the current time by default

Navigation Bar----Personal Center Dropdown

【2022-05-31】JS逆向之易企秀

cmd(命令行)操作或连接mysql数据库,以及创建数据库与表

解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER

【云驻共创】HCSD大咖直播–就业指南

ClickHouse 创建数据库建表视图字典 SQL

Advanced c language: pointers (5)
随机推荐
The most powerful and most commonly used SQL statements in history
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
cnpm installation steps
解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER
鳄梨价格数据集(Avocado Prices)
IDEA 连接 数据库
cnpm安装步骤
Difference between cookie and session
MySQL删除表数据 MySQL清空表命令 3种方法
Gxlcms有声小说系统/小说听书系统源码
Go1.18升级功能 - 泛型 从零开始Go语言
The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
【零代码工具】15 款企业级零代码开发平台推荐,总有一款是你心仪的
2022.7.27
PhpMetrics 使用
Regular expression syntax and usage
IDEA2021.2安装与配置(持续更新)
d违反常了吗
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题