Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Rollback is used to rollback a transaction without returning an error. Rollback = errors.New("Just rollback") )
Functions ¶
func WithTx ¶
WithTx starts a transaction and run fn. If no error is returned, the transaction is committed. Otherwise it is rollbacked and the error is returned to the caller (except returning Rollback, which will rollback the transaction but not return error).
Inside one can use OnCommitted/OnFinalised to add functions to be called after tx end.
Types ¶
type Queryer ¶
type Queryer interface { ExecContext(context.Context, string, ...interface{}) (sql.Result, error) PrepareContext(context.Context, string) (*sql.Stmt, error) QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) QueryRowContext(context.Context, string, ...interface{}) *sql.Row }
Queryer abstracts sql.DB/sql.Conn/sql.Tx .
type TxContext ¶
type TxContext struct {
// contains filtered or unexported fields
}
TxContext contains transaction context.
func CurTxContext ¶
CurTxContext returns current TxContext in transaction.
func MustCurTxContext ¶
MustCurTxContext is the `must` version of CurTxContext.
func (*TxContext) Local ¶
func (txCtx *TxContext) Local(key interface{}) interface{}
Local gets tx local variable.
func (*TxContext) OnCommitted ¶
func (txCtx *TxContext) OnCommitted(fn func())
OnCommitted adds a function which will be only called after the transaction has been successful committed. The invocation order of OnCommitted functions just like defer functions.
func (*TxContext) OnFinalised ¶
func (txCtx *TxContext) OnFinalised(fn func())
OnFinalised adds a function which will be called after the transaction ended (either committed or rollback). The invocation order of OnFinalised functions just like defer functions.