Documentation ¶
Overview ¶
Package dbtx provides convenience functions for database transactions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 )
Exclusivity options.
func (Exclusivity) PgTxLock ¶
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 ¶
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 ¶
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 ¶
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.