transactor

package module
Version: v0.0.0-...-8e7d60d Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2020 License: MIT Imports: 3 Imported by: 0

README

Transactor

Transactor is a simple transaction manager that handles opening/committing transactions.

Usage

package main

import (
    "context"
    
    "github.com/jmoiron/sqlx"
    "github.com/yurykabanov/go-transactor"
)
   
var (
  db *sqlx.DB
)

func doSomething(ctx context.Context) error {
  var data struct{}

  // Transaction passed implicitly can be extracted via transactor.ExtractTx.
  tx := transactor.ExtractTx(ctx)

  // It is useful to write methods that could be called either in transaction
  // or without explicit one while keeping function signature as simple as possible.
  if tx == nil {
    return tx.GetContext(ctx, &data, "SELECT * FROM some_table LIMIT 1")
  } else {
    return db.GetContext(ctx, &data, "SELECT * FROM some_table LIMIT 1")
  }
}       

func main() {
  db, _ = sqlx.Open("driver_name", "dsn")

  txm := transactor.NewTxManager(db)

  err = txm.WithinTx(context.Background(), func(ctx context.Context, tx *sqlx.Tx) error {
    // 1. Explicitly passed transaction
    var data struct{}
    err := tx.GetContext(ctx, &data, "SELECT * FROM some_table LIMIT 1")
    if err != nil {
      return err
    }

    // 2. Implicitly passed transaction (can be obtained via transactor.ExtractTx)  
    return doSomething(ctx)
  })
  if err != nil {
    panic(err)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTx

func ExtractTx(ctx context.Context) *sqlx.Tx

Types

type TxFunc

type TxFunc func(ctx context.Context, tx *sqlx.Tx) error

TxFunc is an alias for function passed to TxManager.WithinTx.

type TxManager

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

TxManager is a simple transaction manager that handles opening/committing transactions.

func NewTxManager

func NewTxManager(db *sqlx.DB) *TxManager

func (*TxManager) WithinTx

func (txm *TxManager) WithinTx(ctx context.Context, txFunc TxFunc) (err error)

WithinTx executes txFunc within transaction opening new transaction and committing it.

Passes transaction implicitly via context. Implicit transaction can be obtained using ExtractTx function.

txFunc should not commit or rollback passed transaction. txFunc can either use the transaction that is explicitly provided or use the transaction that is implicitly provided via context (through ExtractTx function), i.e.

txm.WithinTx(originalCtx, func(ctx context.Context, tx *sqlx.Tx) error {
  tx.DoSomething() // ok
  ExtractTx(ctx).DoSomething() // ok
})

Nested calls neither open new transactions, nor close existing one. Transaction will be committed (or rollbacked) when txFunc of the outermost WithinTx call is finished, i.e.

txm.WithinTx(originalCtx, func(outerCtx context.Context, outerTx *sqlx.Tx) error {
  // before: new tx is opened and stored in ctx

  // outerCtx := originalCtx + tx

  txm.WithinTx(outerCtx, func(innerCtx context.Context, innerTx *sqlx.Tx) error {
    // before: no new tx is opened

    // innerCtx == outerCtx - due to nested call
    // innerTx == outerTx - due to nested call

    // after: tx is not committed (or rollbacked)
  })

  // after: tx is committed (or rollbacked)
})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL