stdtx

package
v0.0.185 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 9 Imported by: 0

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 NoTestForMaxQueryPlanCosts added in v0.0.122

func NoTestForMaxQueryPlanCosts(ctx context.Context) bool

NoTestForMaxQueryPlanCosts returns whether the cost check is disabled.

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.

func WithNoTestForMaxQueryPlanCosts added in v0.0.122

func WithNoTestForMaxQueryPlanCosts(ctx context.Context) context.Context

WithNoTestForMaxQueryPlanCosts allow disabling the plan cost check.

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.

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.

Directories

Path Synopsis
Package stdtxpgxv5 implements the stdtx.Driver for pgx/v5 postgres driver.
Package stdtxpgxv5 implements the stdtx.Driver for pgx/v5 postgres driver.
Package stdtxtest provides testing utilities for our standard transactions.
Package stdtxtest provides testing utilities for our standard transactions.

Jump to

Keyboard shortcuts

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