sqlx: optimize transacation process logic

pull/351/head
Michael Li 2 years ago
parent bd614e7066
commit 62f348a714
No known key found for this signature in database

@ -29,22 +29,23 @@ func (s *sqlxServant) with(handle func(tx *sqlx.Tx) error) error {
if err != nil {
return err
}
if err = handle(tx); err == nil {
return tx.Commit()
defer tx.Rollback()
if err = handle(tx); err != nil {
return err
}
return tx.Rollback()
return tx.Commit()
}
func (s *sqlxServant) withTx(ctx context.Context, opts *sql.TxOptions, handle func(*sqlx.Tx) error) error {
tx, err := s.db.BeginTxx(ctx, opts)
if err != nil {
return err
}
defer tx.Rollback()
if err = handle(tx); err == nil {
return tx.Commit()
return err
}
return tx.Rollback()
return tx.Commit()
}
func newSqlxServant(db *sqlx.DB) *sqlxServant {

Loading…
Cancel
Save