txmgr

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 4 Imported by: 0

README

Transaction Manager (txmgr)

A database-agnostic transaction management package that provides a clean and consistent way to handle database transactions in Go applications.

Overview

The txmgr package implements a transaction management system that is independent of specific database implementations. It provides interfaces and types for managing transaction lifecycles, isolation levels, and operation modes while supporting nested transactions.

Features

  • Database-agnostic transaction management
  • Support for different transaction isolation levels
  • Configurable transaction modes (read-only/read-write)
  • Advisory locking support
  • Nested transaction handling
  • Clean interface-based design

Core Components

Interfaces
  • ITransactionInformer: Provides transaction state information (implemented for PostgreSQL in pgdb package)
  • ITransactionBeginner: Handles transaction initiation (implemented for PostgreSQL in pgdb package)
  • ITransactionManager: Main interface for transaction management

Usage

Creating a Transaction Manager
tm := txmgr.New(beginner, informer)
Starting a Transaction
err := tm.Begin(ctx, func(ctxTx context.Context) error {
    // Your transactional code here
    return nil
}, txmgr.WithTransactionLevel(txmgr.TxReadCommitted),
   txmgr.WithTransactionMode(txmgr.TxReadWrite),
   txmgr.WithLock())
Configuration Options

Transaction behavior can be customized using option functions:

// Set isolation level
txmgr.WithTransactionLevel(txmgr.TxReadCommitted)

// Set transaction mode
txmgr.WithTransactionMode(txmgr.TxReadWrite)

// Enable advisory locking
txmgr.WithLock()
Nested Transactions

The package handles nested transactions by maintaining consistent isolation levels and modes:

  • If a transaction is already started, the package verifies that the requested isolation level and mode match the current transaction
  • If they match, the function executes within the current transaction
  • If they don't match, an error is returned

Important Notes

  1. Transaction options (isolation level and mode) cannot be changed once a transaction has started
  2. The Lock option is advisory and its implementation depends on the underlying database driver
  3. Default isolation level is TxReadCommitted
  4. Default transaction mode is TxReadWrite
  5. The concrete implementations of ITransactionInformer and ITransactionBeginner for PostgreSQL are provided in the pgdb package

Documentation

Overview

Package txmgr is a generated GoMock package.

Package txmgr implements a database-agnostic transaction manager.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ITransactionBeginner

type ITransactionBeginner interface {
	Begin(ctx context.Context, f func(ctxTr context.Context) error, opts Options) error
	// BeginTx starts a transaction. If transaction is already started - increment nested level.
	BeginTx(ctx context.Context, opts Options) (context.Context, ITransactionFinisher, error)
	// WithoutTransaction returns context without transaction.
	WithoutTransaction(ctx context.Context) context.Context
}

ITransactionBeginner interface for starting transactions. Implemented in pgdb package.

type ITransactionFinisher

type ITransactionFinisher interface {
	// Commit commits the transaction.
	Commit(ctx context.Context) error
	// Rollback rolls back the transaction.
	Rollback(ctx context.Context) error
}

ITransactionFinisher interface for finishing transactions. Implemented in pgdb package.

type ITransactionInformer

type ITransactionInformer interface {
	// InTransaction returns true if transaction is started.
	InTransaction(ctx context.Context) bool
	// TransactionOptions returns transaction parameters.
	TransactionOptions(ctx context.Context) Options
}

ITransactionInformer interface for transaction information. Implemented in pgdb package.

type ITransactionManager

type ITransactionManager interface {
	// Begin starts a transaction. If transaction is already started - increment nested level.
	Begin(ctx context.Context, f func(ctxTr context.Context) error, opts ...Option) error
	// BeginTx starts a transaction. If transaction is already started - increment nested level.
	BeginTx(ctx context.Context, opts ...Option) (context.Context, ITransactionFinisher, error)
	// WithoutTransaction returns context without transaction.
	WithoutTransaction(ctx context.Context) context.Context
}

ITransactionManager interface for managing database transactions. Located here at the implementation point for convenient use in other packages.

type MockITransactionBeginner

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

MockITransactionBeginner is a mock of ITransactionBeginner interface.

func NewMockITransactionBeginner

func NewMockITransactionBeginner(ctrl *gomock.Controller) *MockITransactionBeginner

NewMockITransactionBeginner creates a new mock instance.

func (*MockITransactionBeginner) Begin

Begin mocks base method.

func (*MockITransactionBeginner) BeginTx

BeginTx mocks base method.

func (*MockITransactionBeginner) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockITransactionBeginner) WithoutTransaction

func (m *MockITransactionBeginner) WithoutTransaction(ctx context.Context) context.Context

WithoutTransaction mocks base method.

type MockITransactionBeginnerMockRecorder

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

MockITransactionBeginnerMockRecorder is the mock recorder for MockITransactionBeginner.

func (*MockITransactionBeginnerMockRecorder) Begin

func (mr *MockITransactionBeginnerMockRecorder) Begin(ctx, f, opts any) *gomock.Call

Begin indicates an expected call of Begin.

func (*MockITransactionBeginnerMockRecorder) BeginTx

func (mr *MockITransactionBeginnerMockRecorder) BeginTx(ctx, opts any) *gomock.Call

BeginTx indicates an expected call of BeginTx.

func (*MockITransactionBeginnerMockRecorder) WithoutTransaction

func (mr *MockITransactionBeginnerMockRecorder) WithoutTransaction(ctx any) *gomock.Call

WithoutTransaction indicates an expected call of WithoutTransaction.

type MockITransactionFinisher

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

MockITransactionFinisher is a mock of ITransactionFinisher interface.

func NewMockITransactionFinisher

func NewMockITransactionFinisher(ctrl *gomock.Controller) *MockITransactionFinisher

NewMockITransactionFinisher creates a new mock instance.

func (*MockITransactionFinisher) Commit

Commit mocks base method.

func (*MockITransactionFinisher) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockITransactionFinisher) Rollback

func (m *MockITransactionFinisher) Rollback(ctx context.Context) error

Rollback mocks base method.

type MockITransactionFinisherMockRecorder

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

MockITransactionFinisherMockRecorder is the mock recorder for MockITransactionFinisher.

func (*MockITransactionFinisherMockRecorder) Commit

Commit indicates an expected call of Commit.

func (*MockITransactionFinisherMockRecorder) Rollback

Rollback indicates an expected call of Rollback.

type MockITransactionInformer

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

MockITransactionInformer is a mock of ITransactionInformer interface.

func NewMockITransactionInformer

func NewMockITransactionInformer(ctrl *gomock.Controller) *MockITransactionInformer

NewMockITransactionInformer creates a new mock instance.

func (*MockITransactionInformer) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockITransactionInformer) InTransaction

func (m *MockITransactionInformer) InTransaction(ctx context.Context) bool

InTransaction mocks base method.

func (*MockITransactionInformer) TransactionOptions

func (m *MockITransactionInformer) TransactionOptions(ctx context.Context) Options

TransactionOptions mocks base method.

type MockITransactionInformerMockRecorder

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

MockITransactionInformerMockRecorder is the mock recorder for MockITransactionInformer.

func (*MockITransactionInformerMockRecorder) InTransaction

func (mr *MockITransactionInformerMockRecorder) InTransaction(ctx any) *gomock.Call

InTransaction indicates an expected call of InTransaction.

func (*MockITransactionInformerMockRecorder) TransactionOptions

func (mr *MockITransactionInformerMockRecorder) TransactionOptions(ctx any) *gomock.Call

TransactionOptions indicates an expected call of TransactionOptions.

type MockITransactionManager

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

MockITransactionManager is a mock of ITransactionManager interface.

func NewMockITransactionManager

func NewMockITransactionManager(ctrl *gomock.Controller) *MockITransactionManager

NewMockITransactionManager creates a new mock instance.

func (*MockITransactionManager) Begin

func (m *MockITransactionManager) Begin(ctx context.Context, f func(context.Context) error, opts ...Option) error

Begin mocks base method.

func (*MockITransactionManager) BeginTx

BeginTx mocks base method.

func (*MockITransactionManager) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockITransactionManager) WithoutTransaction

func (m *MockITransactionManager) WithoutTransaction(ctx context.Context) context.Context

WithoutTransaction mocks base method.

type MockITransactionManagerMockRecorder

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

MockITransactionManagerMockRecorder is the mock recorder for MockITransactionManager.

func (*MockITransactionManagerMockRecorder) Begin

func (mr *MockITransactionManagerMockRecorder) Begin(ctx, f any, opts ...any) *gomock.Call

Begin indicates an expected call of Begin.

func (*MockITransactionManagerMockRecorder) BeginTx

func (mr *MockITransactionManagerMockRecorder) BeginTx(ctx any, opts ...any) *gomock.Call

BeginTx indicates an expected call of BeginTx.

func (*MockITransactionManagerMockRecorder) WithoutTransaction

func (mr *MockITransactionManagerMockRecorder) WithoutTransaction(ctx any) *gomock.Call

WithoutTransaction indicates an expected call of WithoutTransaction.

type Option

type Option func(*Options)

Option transaction manager option function.

func WithLock

func WithLock() Option

WithLock enables object locking.

func WithTransactionLevel

func WithTransactionLevel(level TransactionLevel) Option

WithTransactionLevel sets the transaction isolation level.

func WithTransactionMode

func WithTransactionMode(mode TransactionMode) Option

WithTransactionMode sets the transaction mode.

type Options

type Options struct {
	// Level defines the transaction isolation level.
	Level TransactionLevel
	// Mode defines the transaction operation mode.
	Mode TransactionMode
	// Lock indicates if object locking is required.
	// This is an advisory option and the implementation decides what to lock.
	// In most cases it means SELECT ... FOR UPDATE.
	Lock bool
}

Options represents transaction manager configuration options.

type TransactionLevel

type TransactionLevel int

TransactionLevel defines the database transaction isolation level.

const (
	TxLevelDefault    TransactionLevel = 0 // Default is TxReadCommitted
	TxReadUncommitted TransactionLevel = 1 // Lowest isolation level
	TxReadCommitted   TransactionLevel = 2 // Prevents dirty reads
	TxRepeatableRead  TransactionLevel = 3 // Prevents non-repeatable reads
	TxSerializable    TransactionLevel = 4 // Highest isolation level
)

Transaction isolation levels from lowest to highest isolation.

type TransactionManager

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

TransactionManager handles database transactions.

func New

func New(tmBeginner ITransactionBeginner, tmImplementator ITransactionInformer) *TransactionManager

New creates a new TransactionManager.

func (*TransactionManager) Begin

func (tm *TransactionManager) Begin(ctx context.Context, f func(ctxTr context.Context) error, opts ...Option) error

Begin starts a new transaction and executes the function.

func (*TransactionManager) BeginTx

BeginTx starts a new transaction.

func (*TransactionManager) WithoutTransaction

func (tm *TransactionManager) WithoutTransaction(ctx context.Context) context.Context

WithoutTransaction returns context without transaction.

type TransactionMode

type TransactionMode int

TransactionMode defines the database transaction access mode.

const (
	TxModeDefault TransactionMode = 0 // TxReadWrite
	TxReadOnly    TransactionMode = 1
	TxReadWrite   TransactionMode = 2
)

Transaction operation modes.

Jump to

Keyboard shortcuts

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