future-architect/go-twowaysql

update panic recover in Twowaysql.Transaction.

future-taga opened this issue · 0 comments

Improved recover and automatic rollback in the panic

func (t *Twowaysql) Transaction(ctx context.Context, fn func(tx TwowaysqlTx) error) error {
tx, err := t.Begin(ctx)
if err != nil {
return err
}
if err := fn(*tx); err != nil {
if rerr := tx.Rollback(); rerr != nil {
return fmt.Errorf("failed rollback %v: %w", rerr, err)
}
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}