tx

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: Apache-2.0 Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) (*sql.Tx, bool)

FromContext returns the transaction from the context.

Types

type Closure

type Closure func(context.Context, *sql.Tx) error

Closure is a function that is executed within a transaction. The function should not commit or rollback the transaction.

type Middleware

type Middleware func(Closure) Closure

Middleware is a function that is executed before and after the closure.

type Opener

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

Opener is an interface for database connections that support opening transactions. This interface is implemented by *sql.DB and *sql.Conn.

type Transactor

type Transactor interface {
	// InTx executes the given closure within a transaction. The transaction is
	// committed if the closure returns nil, otherwise it is rolled back. It is
	// important that the closure does not commit or rollback the transaction as
	// this is handled by the transactor automatically. Recursively calling InTx
	// will re-use the same transaction.
	InTx(context.Context, Closure) error
}

The transactor is responsible for creating and committing or rolling back transactions.

func NewTransactor

func NewTransactor(o Opener, opts TransactorOptions) Transactor

NewTransactor creates a new Transactor that uses the given a Opener. This function panics if the Opener is nil. The transactor is responsible for creating and committing or rolling back transactions.

type TransactorOptions

type TransactorOptions struct {
	// sql.TxOptions to pass the opener when it begins a transaction. This does
	// not apply to new savepoint transactions (only the root transaction).
	BeginOptions *sql.TxOptions
	// Middlewares to execute before and after the closure. The middlewares are
	// executed in the order they are passed. The middlewares are executed only
	// once per opened transaction, not for each recursive call to InTx.
	BeginMiddlewares []Middleware
	// If true, the transactor will use savepoints to create nested transactions.
	// Make sure the database supports savepoints before enabling this option.
	EnableSavepoints bool
}

Jump to

Keyboard shortcuts

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