当前位置:网站首页>Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
2022-07-30 05:14:00 【繁华哟】
学习笔记,写到哪是哪。
接着上一篇文章: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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 3. Dependency configuration management
- go语言学习笔记四
- DLL description (1)
- go language study notes 4
- Using the GPU parallel computing 】 【 OpenCL&OpenCLUtilty GPU parallel computing
- SVN View Username and Password
- POJ1321 棋盘问题(详解)
- Recursive Optimization of Fibonacci Sequences "Memo Recursion"
- mysql隔离级别
- Requirements design document and the changing role of the product manager
猜你喜欢

Record of problems encountered by the pyinstaller packager

Seven, custom configuration

(RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise

RadonDB MySQL Kubernetes 2.2.0 发布!

Unity踩坑记录 —— GetComponent的使用

oracle触发器的自治事务

为Bitbucket 和 Sourcetree 设置SSL认证

即刻报名|如何降低云上数据分析成本?

Some understanding of YOLOv7

动态规划问题(完结篇)
随机推荐
WPF introduces ttf icon file usage record
字符串问题(下)
mysql隔离级别
双指针问题(中)
Protobuf compound data types, speaking, reading and writing
POJ1321 chessboard problem (detailed explanation)
[3D Detection Series-PointRCNN] Reproduces the PointRCNN code, and realizes the visualization of PointRCNN3D target detection, including the download link of pre-training weights (starting from 0 and
双指针问题(上)
LeetCode Algorithm 328. Parity linked list
WPF study notes "WPF Layout Basics"
Hexagon_V65_Programmers_Reference_Manual (11)
给小白的 PostgreSQL 容器化部署教程(上)
斐波那契数列的递归优化《备忘录递归》
Stimulsoft ReportsJS and DashboardsJS. 2022.3.3
ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
Using the GPU parallel computing 】 【 OpenCL&OpenCLUtilty GPU parallel computing
Detailed explanation of REUSE_ALV_GRID_DISPLAY
Simulation problem (below)
gnss rtcm rtklib Ntrip...
RadonDB MySQL on K8s 2.1.3 发布!