包装事务处理

This commit is contained in:
白茶清欢 2022-12-24 21:37:08 +08:00
parent 932d75f020
commit 158942e381

18
base.go
View File

@ -6,6 +6,7 @@
package mysql
import (
"errors"
"gorm.io/gorm"
)
@ -63,6 +64,23 @@ func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...Set
return cnt, dbInstance.Table(table).Count(&cnt).Error
}
// Tx 执行事务
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:31 2022/12/24
func (b *BaseDao) Tx(dbInstance *gorm.DB, txFunc func(dbInstance *gorm.DB) error) error {
if nil == dbInstance {
return errors.New("db instance is null")
}
tx := b.Begin(dbInstance)
if err := txFunc(tx); nil != err {
_ = b.Rollback(tx)
return err
}
return b.Commit(tx)
}
// Begin 开启事务
//
// Author : go_developer@163.com<白茶清欢>