tx

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrTmplSPFailure = `SavePoint failed. did you pass along the context provided by Begin(...)?`
)
View Source
const (
	FxTransactionExecuterOption = "TransactionExecuterOption"
)

Variables

View Source
var (
	ErrExceededMaxRetries = errors.New("exceeded maximum number of retries")
)
View Source
var Module = &bootstrap.Module{
	Name:       "DB Tx",
	Precedence: bootstrap.DatabasePrecedence,
	Options: []fx.Option{
		fx.Provide(provideGormTxManager),
		fx.Invoke(setGlobalTxManager),
	},
}

Functions

func Begin

func Begin(ctx context.Context, opts ...*sql.TxOptions) (context.Context, error)

Begin start a transaction. the returned context.Context should be used for any transactioanl operations if returns an error, the returned context.Context should be disgarded

func Commit

func Commit(ctx context.Context) (context.Context, error)

Commit commit a transaction. the returned context.Context is the original provided context when Begin is called if returns an error, the returned context.Context should be disgarded

func DoWithDB

func DoWithDB(ctx context.Context, fn func(*gorm.DB) *gorm.DB) error

func ErrIsRetryable

func ErrIsRetryable(err error) bool

The below code is taken from crdb/tx.go in the crdb package

func GormTxWithContext

func GormTxWithContext(ctx context.Context) (tx *gorm.DB)

func NewGormTxContext

func NewGormTxContext(ctx context.Context, db *gorm.DB) context.Context

NewGormTxContext will wrap the given Context and *gorm.DB in a gormTxContext

func Rollback

func Rollback(ctx context.Context) (context.Context, error)

Rollback rollback a transaction. the returned context.Context is the original provided context when Begin is called if returns an error, the returned context.Context should be disgarded

func RollbackTo

func RollbackTo(ctx context.Context, name string) (context.Context, error)

RollbackTo works with SavePoint and have to be within an transaction. the returned context.Context should be used for any transactioanl operations between corresponding SavePoint and RollbackTo if returns an error, the returned context.Context should be disgarded

func SavePoint

func SavePoint(ctx context.Context, name string) (context.Context, error)

SavePoint works with RollbackTo and have to be within an transaction. the returned context.Context should be used for any transactioanl operations between corresponding SavePoint and RollbackTo if returns an error, the returned context.Context should be disgarded

func Transaction

func Transaction(ctx context.Context, tx TxFunc, opts ...*sql.TxOptions) error

Transaction start a transaction as a block, return error will rollback, otherwise to commit.

Types

type DefaultExecuter

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

DefaultExecuter executes the default behaviour for the TxManager and ManualTxManager It is possible to define a custom Executer and swap it with this DefaultExecuter. To do so, see tx/package.go and the TransactionExecuter in the fx.In of the provideGormTxManager

func (*DefaultExecuter) Begin

func (r *DefaultExecuter) Begin(ctx context.Context, db *gorm.DB, opts ...*sql.TxOptions) (context.Context, error)

func (*DefaultExecuter) Commit

func (r *DefaultExecuter) Commit(ctx context.Context) (context.Context, error)

func (*DefaultExecuter) ExecuteTx

func (r *DefaultExecuter) ExecuteTx(ctx context.Context, db *gorm.DB, opt *sql.TxOptions, txFunc TxFunc) error

func (*DefaultExecuter) Rollback

func (r *DefaultExecuter) Rollback(ctx context.Context) (context.Context, error)

func (*DefaultExecuter) RollbackTo

func (r *DefaultExecuter) RollbackTo(ctx context.Context, name string) (context.Context, error)

func (*DefaultExecuter) SavePoint

func (r *DefaultExecuter) SavePoint(ctx context.Context, name string) (context.Context, error)

type GormContext

type GormContext interface {
	DB() *gorm.DB
}

type GormTxManager

type GormTxManager interface {
	TxManager
	WithDB(*gorm.DB) GormTxManager
}

type ManualTxManager

type ManualTxManager interface {
	Begin(ctx context.Context, opts ...*sql.TxOptions) (context.Context, error)
	Rollback(ctx context.Context) (context.Context, error)
	Commit(ctx context.Context) (context.Context, error)
	SavePoint(ctx context.Context, name string) (context.Context, error)
	RollbackTo(ctx context.Context, name string) (context.Context, error)
}

ManualTxManager defines interfaces for manual transaction management if any methods returns an error, the returned context should be disgarded

type TransactionExecuter

type TransactionExecuter interface {
	ExecuteTx(context.Context, *gorm.DB, *sql.TxOptions, TxFunc) error
	Begin(ctx context.Context, db *gorm.DB, opts ...*sql.TxOptions) (context.Context, error)
	Rollback(ctx context.Context) (context.Context, error)
	Commit(ctx context.Context) (context.Context, error)
	SavePoint(ctx context.Context, name string) (context.Context, error)
	RollbackTo(ctx context.Context, name string) (context.Context, error)
}

func NewDefaultExecuter

func NewDefaultExecuter(options ...TransactionExecuterOption) TransactionExecuter

type TransactionExecuterOption

type TransactionExecuterOption func(options *TransactionExecuterOptions)

func MaxRetries

func MaxRetries(maxRetries int, order int) TransactionExecuterOption

MaxRetries will return a TransactionExecuterOption of type OrderedTransactionExecuterOption

type TransactionExecuterOptions

type TransactionExecuterOptions struct {
	MaxRetries int
}

type TxContext

type TxContext interface {
	Parent() context.Context
}

type TxFunc

type TxFunc func(ctx context.Context) error

type TxManager

type TxManager interface {
	Transaction(ctx context.Context, tx TxFunc, opts ...*sql.TxOptions) error
}

Jump to

Keyboard shortcuts

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