transaction

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package transaction defines an application transaction boundary independent of a storage driver. Pass the callback context to every participating operation.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoTransaction    = errors.New("pfw: no transaction in context")
	ErrClosed           = errors.New("pfw: transaction scope is closed")
	ErrRollbackOnly     = errors.New("pfw: transaction marked for rollback")
	ErrDifferentManager = errors.New("pfw: context belongs to another transaction manager")
)

Functions

func Do

func Do[T any](ctx context.Context, runner Runner, fn func(context.Context) (T, error)) (T, error)

Do returns a value only after a successful boundary. On any failure the result is its zero value, including when the callback succeeded but commit failed. In a nested call the value is provisional until the outer boundary commits.

Types

type Beginner

type Beginner interface {
	Begin(context.Context) (Transaction, error)
}

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func New

func New(backend Beginner, options Options) (*Manager, error)

func (*Manager) Within

func (m *Manager) Within(ctx context.Context, fn func(context.Context) error) (err error)

Within owns begin/commit/rollback at the outermost call. Inner failures mark the shared scope rollback-only, even if their caller handles the returned error. Panics roll back and propagate unchanged. Work must finish before fn returns; do not retain the context or launch unjoined goroutines from the callback.

type Options

type Options struct{ RollbackTimeout time.Duration }

type Runner

type Runner interface {
	Within(context.Context, func(context.Context) error) error
}

Runner is the application-facing contract. Nested calls on the same manager join the existing transaction, rather than creating savepoints.

type Transaction

type Transaction interface {
	Commit(context.Context) error
	Rollback(context.Context) error
}

Transaction is implemented by storage adapters. Commit must honor the caller context. Rollback receives a separate bounded context for cleanup.

func Current

func Current(ctx context.Context) (Transaction, error)

Current is intended for storage adapters. A context retained after Within returns must fail, rather than silently running work outside the transaction.

Directories

Path Synopsis
Package sql adapts application transactions to database/sql.
Package sql adapts application transactions to database/sql.

Jump to

Keyboard shortcuts

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