ExecuteTx starts a transaction, and runs fn in it.
( ctx context.Context, db *sql.DB, txopts *sql.TxOptions, fn func(*sql.Tx) error, )
| 27 | |
| 28 | // ExecuteTx starts a transaction, and runs fn in it. |
| 29 | func ExecuteTx( |
| 30 | ctx context.Context, db *sql.DB, txopts *sql.TxOptions, fn func(*sql.Tx) error, |
| 31 | ) error { |
| 32 | // Start a transaction. |
| 33 | tx, err := db.BeginTx(ctx, txopts) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | return ExecuteInTx(tx, func() error { return fn(tx) }) |
| 38 | } |
| 39 | |
| 40 | // ExecuteInTx runs fn inside tx which should already have begun. |
| 41 | func ExecuteInTx(tx driver.Tx, fn func() error) (err error) { |