在编写业务代码的过程中,如果涉及到多张表的更新操作,为了确保数据的一致性,我们会在业务代码的过程中加上事务的控制,那么针对go 语言中,如果我们使用gorm框架改如何操作呢?
func NewTransaction() *gorm.DB {
return suit.GetGormClient().Begin()
}
tx := s.AlarmDao.NewTransaction()
defer tx.Rollback()
...(具体业务处理)
err = tx.Commit().Error
func (r *Repo) TxUpdateBonus(ctx context.Context, bonus *entity.Bonus, transaction *entity.Transaction) error {
return r.Connection().Transaction(func(tx *gorm.DB) error {
if bonus.ID < 1 {
if err := tx.WithContext(ctx).Create(bonus).Error; err != nil {
return err
}
} else {
if err := tx.WithContext(ctx).Model(bonus).Updates(bonus.Conv2Map()).Error; err != nil {
return err
}
}
if err := tx.WithContext(ctx).Create(transaction).Error; err != nil {
return err
}
return nil
})
}
var dbClient *DbClient
type DbClient struct {
MysqlClient *gorm.DB
}
func NewDbClient() *DbClient {
dbClient = &DbClient{}
dbClient.MysqlClient = suit.GetGormClient()
return dbClient
}
type contextTxKey struct{}
type TxFn func(context.Context) error
func ExecTx(ctx context.Context, fn TxFn) error {
return suit.GetGormClient().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
ctx = context.WithValue(ctx, contextTxKey{}, tx)
return fn(ctx)
})
}
func GetTxGorm(ctx context.Context) (db *gorm.DB, ok bool) {
db, ok = ctx.Value(contextTxKey{}).(*gorm.DB)
return
}
err = dao.ExecTx(ctx, func(ctx context.Context) error {
instance.DeletedAt = time.Now()
instance.UpdatedAt = time.Now()
instance.State = constant.InstanceStateDeleteSuccess
err = a.MongodbInstance.UpdateOrInsert(ctx, &instance)
if err != nil {
return err
}
req := &middleware.DeleteMongoDBDBInstanceRequest{}
err = a.MiddlewareClient.DeleteMongoDBDBInstance(ctx, common, req)
if err != nil {
return err
}
err = a.deleteAndRefundResource(ctx, &instance, true, false)
return err
})
func (a *MongodbInstance) dbClient(ctx context.Context) *gorm.DB {
if tx, ok := dao.GetTxGorm(ctx); ok {
return tx
}
return a.DB.MysqlClient
}
func (a *MongodbInstance) UpdateOrInsert(ctx context.Context, p *po.MongodbInstance) (err error) {
client := a.dbClient(ctx)
if p.Id == 0 {
err = client.Table(po.MongodbInstance{}.TableName()).Create(p).Error
return
}
err = client.Table(po.MongodbInstance{}.TableName()).Select("*").Omit("created_at").Updates(p).Error
return
}