Documentation
¶
Index ¶
- Variables
- func HasTransaction(ctx context.Context) bool
- func IsSerializationError(err error) bool
- func TxFromContext(ctx context.Context) pgx.Tx
- func TxToContext(ctx context.Context, tx pgx.Tx) context.Context
- func WithRetry(ctx context.Context, config RetryConfig, fn func(ctx context.Context) error) error
- type Executor
- type ExecutorProvider
- type Manager
- type RetryConfig
- type TxFunc
- type TxManager
- type TxManagerOption
- type TxProvider
- type TxRunner
Constants ¶
This section is empty.
Variables ¶
var ErrSerialization = errors.New("transaction serialization failure")
ErrSerialization is returned when a transaction fails due to serialization failure or deadlock.
Functions ¶
func HasTransaction ¶
HasTransaction checks if the context contains a transaction.
func IsSerializationError ¶
IsSerializationError checks if the error is a serialization failure or deadlock. These errors are retryable.
func TxFromContext ¶
TxFromContext returns the transaction from the context. Returns nil if no transaction is present. Repositories can use this to check if they should use a transaction or a direct database connection.
func TxToContext ¶
TxToContext returns a new context with the transaction stored in it. This allows repositories to automatically use the transaction without explicitly passing it around.
Types ¶
type Executor ¶
type Executor interface {
Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}
Executor is a common interface for executing database queries. It is implemented by *pgx.Conn, pgx.Tx, and *pgxpool.Pool. Repositories should depend on this interface instead of concrete types to support transactions seamlessly.
type ExecutorProvider ¶ added in v0.2.0
ExecutorProvider provides an Executor for the given context. Repositories should typically depend on this interface instead of *TxManager.
type Manager ¶ added in v0.2.0
type Manager interface {
ExecutorProvider
TxRunner
}
Manager combines transaction execution and executor lookup.
type RetryConfig ¶
type RetryConfig struct {
// MaxRetries is the maximum number of retry attempts.
// Default is 3 if not specified.
MaxRetries int
// InitialInterval is the initial interval between retries.
// Default is 100ms if not specified.
InitialInterval time.Duration
// MaxInterval is the maximum interval between retries.
// Default is 1s if not specified.
MaxInterval time.Duration
// Multiplier is the exponential backoff multiplier.
// Default is 2.0 if not specified.
Multiplier float64
}
RetryConfig holds configuration for transaction retry logic.
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns the default retry configuration.
type TxManager ¶
type TxManager struct {
// contains filtered or unexported fields
}
TxManager is a transaction manager that handles transaction lifecycle and nested transaction calls.
func NewTxManager ¶
func NewTxManager(pool *pgxpool.Pool, opts ...TxManagerOption) *TxManager
NewTxManager creates a new transaction manager.
func (*TxManager) ExecInTx ¶
func (tm *TxManager) ExecInTx(ctx context.Context, fn func(ctx context.Context, exec Executor) error) error
ExecInTx is a helper that executes a function in a transaction if one exists, otherwise uses the pool directly. This is useful for queries that don't need transaction guarantees but should participate in a transaction if available.
func (*TxManager) GetExecutor ¶
GetExecutor returns an Executor for the given context. If a transaction exists in the context, it returns the transaction. Otherwise, it returns the pool (for direct queries without transaction).
Repositories should use this method to obtain an Executor instead of storing a reference to the pool or transaction directly.
func (*TxManager) GetPool ¶
GetPool returns the underlying pool. Use this only for operations that must not be part of a transaction.
func (*TxManager) WithTx ¶
WithTx executes the given function within a database transaction.
If a transaction already exists in the context, it will be reused (nested call). Otherwise, a new transaction is created.
The transaction is automatically committed on success or rolled back on error. Serialization errors (40001) and deadlock errors (40P01) are automatically retried.
type TxManagerOption ¶
type TxManagerOption func(*TxManager)
TxManagerOption is a functional option for configuring TxManager.
func WithRetryConfig ¶
func WithRetryConfig(cfg RetryConfig) TxManagerOption
WithRetryConfig sets the retry configuration for the transaction manager.
func WithTracer ¶
func WithTracer(tracer trace.Tracer) TxManagerOption
WithTracer sets the OpenTelemetry tracer for the transaction manager.
func WithoutTx ¶
func WithoutTx() TxManagerOption
WithoutTx disables transaction management. This is useful for testing or read-only operations.
type TxProvider ¶
TxProvider is an interface for obtaining a database connection. This is typically implemented by pgxpool.Pool.
type TxRunner ¶ added in v0.2.0
type TxRunner interface {
WithTx(ctx context.Context, fn TxFunc) error
ExecInTx(ctx context.Context, fn func(ctx context.Context, exec Executor) error) error
}
TxRunner executes work in a transaction-aware way. Services that coordinate multiple repositories should typically depend on this interface.