gormtx

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2022 License: MIT Imports: 8 Imported by: 0

README

gormtx

gorm 事务管理

接口:tx_manager.go

实现:gorm_tx_manager.go

测试:gorm_tx_manager_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBTxManager added in v1.0.3

type DBTxManager interface {
	// OpenMainTx 开启 main库 事物
	OpenMainTx(ctx context.Context, opts ...DBTxOpt) (context.Context, uint64)
	// CloseMainTx 关闭 main库 事务
	CloseMainTx(ctx context.Context, txid uint64, err *error)
	// MainDB 获取 main db,如果已经开启 main db tx,则返回 main db tx
	MainDB(ctx context.Context) *gorm.DB
	// BackupDB 获取 Backup db
	BackupDB() *gorm.DB
	// AutoDB 获取 db,如果已经开启 main db tx,则返回 db tx,否则 返回 backup db
	AutoDB(ctx context.Context) *gorm.DB
	// MustMainTx 获取 main db tx,如果已经开启 main db tx,则返回 main db tx,否则 panic
	MustMainTx(ctx context.Context) *gorm.DB
}

type DBTxOpt

type DBTxOpt func(opt *Option)

func StartupNewDBTx

func StartupNewDBTx() DBTxOpt

StartupNewDBTx 开启一个新的事务

type GormTxManager

type GormTxManager struct {
	// contains filtered or unexported fields
}

GormTxManager db 事务管理器 mainDB, backupDB *gorm.DB // 默认主从库实例 tid2Tx sync.Map // tid(事务ID) 对应的 事务

func NewGormTxManager

func NewGormTxManager(mainDB, backupDB *gorm.DB) *GormTxManager

func NewGormTxManagerWithClauses added in v1.0.1

func NewGormTxManagerWithClauses(db *gorm.DB) *GormTxManager

NewGormTxManagerWithClauses 使用 DBResolver 读写分离

func (*GormTxManager) AutoDB

func (s *GormTxManager) AutoDB(ctx context.Context) *gorm.DB

AutoDB 获取 db,如果已经开启 main db tx,则返回 db tx,否则 返回 backup db

func (*GormTxManager) BackupDB

func (s *GormTxManager) BackupDB() *gorm.DB

BackupDB 获取 Backup db

func (*GormTxManager) CloseMainTx

func (s *GormTxManager) CloseMainTx(ctx context.Context, txid uint64, err *error)

CloseMainTx 关闭 main库 事务 参数: ctx: 开启 main库 事务返回的新的context txid: 开启 main库 事务返回的事务ID err: 判断是提交事务还是回滚事务

func (*GormTxManager) MainDB

func (s *GormTxManager) MainDB(ctx context.Context) *gorm.DB

MainDB 获取 main db,如果已经开启 main db tx,则返回 main db tx

func (*GormTxManager) MustMainTx

func (s *GormTxManager) MustMainTx(ctx context.Context) *gorm.DB

MustMainTx 获取 main db tx,如果已经开启 main db tx,则返回 main db tx,否则 panic

func (*GormTxManager) OpenMainTx

func (s *GormTxManager) OpenMainTx(ctx context.Context, opts ...DBTxOpt) (context.Context, uint64)

OpenMainTx 开启 main库 事务 return 新的context 与 事务ID

type NameDB

type NameDB struct {
	Name string
	DB   *gorm.DB
}

type Option

type Option struct {
	StartupNewTx bool
}

type Persistencer added in v1.0.3

type Persistencer interface {
	DBTxManager() DBTxManager
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL