Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type TxManager ¶
type TxManager struct {
// contains filtered or unexported fields
}
TxManager is a simple transaction manager that handles opening/committing transactions.
func NewTxManager ¶
func (*TxManager) WithinTx ¶
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) })