dbtx

package
v0.0.0-...-2597ab7 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package dbtx provides convenience functions for database transactions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, b Beginner, opts *sql.TxOptions, fns ...Func) error

Do is a convenience wrapper for Transactor{b}.Do(). Code that repeatedly calls Do() MAY wish to use a Transactor to avoid having to constantly pass the Beginner, and to force users to wrap all access in transactions, but both will always be identical.

func PgLockKey

func PgLockKey(key string) int64

PgLockKey returns an int64 for use as a key in obtaining a PostgreSQL advisory lock. The returned value is derived from interpretting the first 8 bytes of the sha256 sum of key as an int64.

Types

type Beginner

type Beginner interface {
	BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
}

A Beginner can begin transactions. Both sql.Conn and sql.DB are Beginners.

type Exclusivity

type Exclusivity int

Exclusivity defines a type of exclusivity for locks. How each type is treated is dependent on the function which it is passed.

const (
	UndefinedExclusivity Exclusivity = iota
	Exclusive
	Shared
)

Exclusivity options.

func (Exclusivity) PgTxLock

func (e Exclusivity) PgTxLock(ctx context.Context, tx *sql.Tx, key int64) error

PgTxLock obtains a PostgreSQL transaction-level lock; i.e. either a [pg_advisory_xact_lock] or the _shared equivalent if e is Exclusive or Shared, respectively.

Such locks can't be explicitly unlocked and are automatically released at the end of the transaction. Transactions using PgTxLock() SHOULD therefore be short-lived and limited to the minimal set of isolated logic—this is why Do() accepts multiple functions.

[pg_advisory_xact_lock] https://www.postgresql.org/docs/15/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS

func (Exclusivity) TryPgTxLock

func (e Exclusivity) TryPgTxLock(ctx context.Context, tx *sql.Tx, key int64) (bool, error)

TryPgTxLock tries to obtain a lock, similarly to PgTxLock(). Failure to obtain the lock does not constitute an error, and the returned boolean MUST be checked.

type Func

type Func func(*sql.Tx) error

A Func is a function that performs actions within the scope of a single database transaction. It MUST NOT call Commit() nor Rollback().

type Transactor

type Transactor struct {
	Beginner
}

A Transactor manages transactions from beginning to end, passing off all application-specific logic to a set of Funcs. Unlike use of the top-level Do() function, wrapping a database connection in a Transactor forces all database access to be in transactions.

func (Transactor) Do

func (t Transactor) Do(ctx context.Context, opts *sql.TxOptions, fns ...Func) error

Do begins one new transaction on the Beginner per Func fn() in fns. The transaction is commited if fn() returns a nil error, otherwise it is rolled back, the error propagated, and all further Funcs ignored.

See PgTxLock() re unlocking as rationale for accepting multiple functions. Note, however, that Do() is agnostic to the underlying database type.

Jump to

Keyboard shortcuts

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