// WithTx runs the given function in a transaction.
WithTx runs the given function in a transaction.
// WithTx runs the given function in a transaction .
func (db *DB) WithTx(ctx context.Context, fn TxFunc) error {
	tx, err := db.client.Tx(ctx)
	if err != nil {
		return err
	}
	defer func() {
		if v := recover(); v != nil {
			tx.Rollback()
			panic(v)
		}
	}()
	if err := fn(tx); err != nil {
		if rerr := tx.Rollback(); rerr != nil {
			err = fmt.Errorf("rolling back transaction: %w", rerr)
		}
		return err
	}
	if err := tx.Commit(); err != nil {
		return fmt.Errorf("committing transaction: %w", err)
	}
	return nil
}
func ( db * DB ) WithTx ( ctx context . Context , fn TxFunc ) error { tx , err := db . client . Tx ( ctx ) if err != nil { return err } defer func ( ) { if v := recover ( ) ; v != nil { tx . Rollback ( ) panic ( v ) } } ( ) if err := fn ( tx ) ; err != nil { if rerr := tx . Rollback ( ) ; rerr != nil { err = fmt . Errorf ( " " , rerr ) } return err } if err := tx . Commit ( ) ; err != nil { return fmt . Errorf ( " " , err ) } return nil }
WithTx runs the given function in a transaction.
WithTx runs the given function in a transaction .