stdtx

package
v0.0.119 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2025 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package stdtx provides a standardized way to handle database transactions.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyInTransactionScope = errors.New("attempt to transact while transaction was already detected")

ErrAlreadyInTransactionScope is returned when transacting has detected that somewhere up the context call chain a transaction was already started.

Functions

func AttemptFromContext

func AttemptFromContext(ctx context.Context) int

AttemptFromContext returns which execution attempt it is. Panics if this information is not present.

func Transact0

func Transact0[TTx any](
	ctx context.Context,
	txr *Transactor[TTx],
	fnc func(ctx context.Context, tx TTx) error,
) (err error)

Transact0 runs Transact1 but without a value to return.

func Transact1

func Transact1[TTx, U any](
	ctx context.Context,
	txr *Transactor[TTx],
	fnc func(ctx context.Context, tx TTx) (U, error),
) (res U, err error)

Transact1 runs fnc in a transaction TTx derived from the provided transactor while returning one value of type U.

Types

type Driver

type Driver[TTx any] interface {
	BeginTx(ctx context.Context) (TTx, error)
	RollbackTx(ctx context.Context, tx TTx) error
	CommitTx(ctx context.Context, tx TTx) error

	SerializationFailureCodes() []string
	SerializationFailureMaxRetries() int

	TxDoneError() error
}

Driver abstracts the sql implementation details for the transactor to function.

func NewPgxV5Driver

func NewPgxV5Driver(db *pgxpool.Pool, txAccessMode pgx.TxAccessMode) Driver[pgx.Tx]

NewPgxV5Driver implements the driver for pgx v5.

type Transactor

type Transactor[TTX any] struct {
	// contains filtered or unexported fields
}

Transactor provides transactions. It can be passed to Transact0 and Transact1 to eaily run code transactionally. A driver can be implemented to support different postgres libraries.

func NewTransactor

func NewTransactor[TTX any](drv Driver[TTX]) *Transactor[TTX]

NewTransactor inits a transactor given the driver.

Jump to

Keyboard shortcuts

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