Documentation ¶
Index ¶
- Constants
- Variables
- func Begin(ctx context.Context, opts ...*sql.TxOptions) (context.Context, error)
- func Commit(ctx context.Context) (context.Context, error)
- func DoWithDB(ctx context.Context, fn func(*gorm.DB) *gorm.DB) error
- func ErrIsRetryable(err error) bool
- func GormTxWithContext(ctx context.Context) (tx *gorm.DB)
- func NewGormTxContext(ctx context.Context, db *gorm.DB) context.Context
- func Rollback(ctx context.Context) (context.Context, error)
- func RollbackTo(ctx context.Context, name string) (context.Context, error)
- func SavePoint(ctx context.Context, name string) (context.Context, error)
- func Transaction(ctx context.Context, tx TxFunc, opts ...*sql.TxOptions) error
- type DefaultExecuter
- func (r *DefaultExecuter) Begin(ctx context.Context, db *gorm.DB, opts ...*sql.TxOptions) (context.Context, error)
- func (r *DefaultExecuter) Commit(ctx context.Context) (context.Context, error)
- func (r *DefaultExecuter) ExecuteTx(ctx context.Context, db *gorm.DB, opt *sql.TxOptions, txFunc TxFunc) error
- func (r *DefaultExecuter) Rollback(ctx context.Context) (context.Context, error)
- func (r *DefaultExecuter) RollbackTo(ctx context.Context, name string) (context.Context, error)
- func (r *DefaultExecuter) SavePoint(ctx context.Context, name string) (context.Context, error)
- type GormContext
- type GormTxManager
- type ManualTxManager
- type TransactionExecuter
- type TransactionExecuterOption
- type TransactionExecuterOptions
- type TxContext
- type TxFunc
- type TxManager
Constants ¶
const (
ErrTmplSPFailure = `SavePoint failed. did you pass along the context provided by Begin(...)?`
)
const (
FxTransactionExecuterOption = "TransactionExecuterOption"
)
Variables ¶
var (
ErrExceededMaxRetries = errors.New("exceeded maximum number of retries")
)
Functions ¶
func Begin ¶
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 ¶
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 ErrIsRetryable ¶
The below code is taken from crdb/tx.go in the crdb package
func NewGormTxContext ¶
NewGormTxContext will wrap the given Context and *gorm.DB in a gormTxContext
func Rollback ¶
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 ¶
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
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) RollbackTo ¶
type GormContext ¶
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
}