Documentation
¶
Overview ¶
Package sqlxtransaction handles begin/commit/rollback control flow around business logic executed inside a sqlx transaction.
How It Works ¶
Exec and ExecWithOptions accept a caller-provided ExecFunc and run it inside a sqlx transaction:
- Start a transaction with DB.BeginTxx.
- Execute the provided function with the transaction object.
- Commit if execution succeeds.
- Roll back automatically if execution fails or commit is not reached.
Rollback behavior is deferred and guarded:
- rollback is skipped after a successful commit,
- `sql.ErrTxDone` is ignored during deferred rollback,
- rollback failures are joined with the current error so diagnostics are not lost.
Usage ¶
err := sqlxtransaction.Exec(ctx, db, func(ctx context.Context, tx *sqlx.Tx) error {
// Execute all related SQL operations using tx.
// Return an error to trigger rollback.
return nil
})
if err != nil {
return err
}
For a similar helper based on the standard database/sql package (instead of github.com/jmoiron/sqlx), see: github.com/tecnickcom/nurago/pkg/sqltransaction
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilDB is returned when the provided DB is nil. ErrNilDB = errors.New("db must not be nil") // ErrNilExecFunc is returned when the provided ExecFunc is nil. ErrNilExecFunc = errors.New("exec function must not be nil") )
Functions ¶
Types ¶
type DB ¶
DB defines the transaction entry point required by Exec and ExecWithOptions.