tx

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoTx

func DoTx(ctx context.Context, db *sql.DB, f TxOperationFunc) error

DoTx is a shortcut for calling Do(x, f) on initialized TxDoer with DB field set to given db.

Types

type Tx

func New

func New(ctx context.Context, opts driver.TxOptions, s table.ClosableSession, close func()) (Tx, error)

type TxDoer

type TxDoer struct {
	DB      *sql.DB
	Options *sql.TxOptions
}

TxDoer contains options for retrying transactions.

func (TxDoer) Do

func (d TxDoer) Do(ctx context.Context, f TxOperationFunc) (err error)

Do starts a transaction and calls f with it. If f() call returns a retryable error, it repeats it accordingly to retry configuration that TxDoer's DB driver holds.

Note that callers should mutate state outside f carefully and keeping in mind that f could be called again even if no error returned – transaction commitment can be failed:

var results []int
ydb.DoTx(x, db, TxOperationFunc(func(x context.Context, tx *conn.Tx) error {
    // Reset resulting slice to prevent duplicates when retry occurred.
    results = results[:0]

    rows, err := tx.QueryContext(...)
    if err != nil {
        // handle error
    }
    for rows.Next() {
        results = append(results, ...)
    }
    return rows.Err()
}))

type TxOperationFunc

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

Jump to

Keyboard shortcuts

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