From 7f44f463362f503caa08f99001b58c296fc61e12 Mon Sep 17 00:00:00 2001 From: Hiven Date: Thu, 10 Aug 2023 14:29:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_resource_logic.go | 10 +++++++++- utils/file/upload.go | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/model/gmodel/fs_resource_logic.go b/model/gmodel/fs_resource_logic.go index 19db19ad..e7749e4a 100644 --- a/model/gmodel/fs_resource_logic.go +++ b/model/gmodel/fs_resource_logic.go @@ -52,6 +52,7 @@ func (m *FsResourceModel) RowSelectBuilder(selectData []string) *gorm.DB { // 事务 func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context, connGorm *gorm.DB) error) error { + tx := m.db.Table(m.name).WithContext(ctx).Begin() defer func() { if r := recover(); r != nil { @@ -60,6 +61,7 @@ func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context }() if err := tx.Error; err != nil { + tx.Rollback() return err } @@ -68,7 +70,13 @@ func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context return err } - return tx.Commit().Error + err := tx.Commit().Error + if err != nil { + tx.Rollback() + return err + } + + return err } func (m *FsResourceModel) TableName() string { diff --git a/utils/file/upload.go b/utils/file/upload.go index 07e7c719..d3114273 100644 --- a/utils/file/upload.go +++ b/utils/file/upload.go @@ -67,6 +67,7 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er var uploadBaseRes = UploadBaseRes{} resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn) + err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error { resourceModelTS := gmodel.NewFsResourceModel(connGorm) resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId)