10

一个简单的 GORM 通用事务接口

 3 years ago
source link: https://www.sulinehk.com/post/a-simple-gorm-general-transaction-interface/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一个简单的 GORM 通用事务接口

2020-12-12

本文介绍了一个简单的 GORM 通用事务接口。

GORM Logo

import (
	"database/sql"
	"log"

	"gorm.io/gorm"
)

// Transaction 指定事务必须的三种方法
type Transaction interface {
	Begin(*gorm.DB) (*gorm.DB, error)
	Rollback(*gorm.DB) error
	Commit(*gorm.DB) error
}

type handler func(db *gorm.DB) error

// Do 在事务中执行函数
func Do(db *gorm.DB, transaction Transaction, call handler) error {
	t, err := transaction.Begin(db)
	if err != nil {
		return err
	}
	// panic 时回滚,并打印日志
	defer func() {
		if err := transaction.Rollback(t); err != nil && err != sql.ErrTxDone {
			log.Fatalf("Rollback err: %v", err)
		}
	}()
	if err := call(t); err != nil {
		return err
	}
	// 没有错误才 commit
	if err := transaction.Commit(t); err != nil {
		return err
	}
	return nil
}

详细说明请看代码注释。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK