mirror of
https://github.com/redis/go-redis.git
synced 2025-04-19 07:22:17 +03:00
Ensure Watch closes the transaction on panic
This commit is contained in:
parent
a8e8bf1504
commit
4ceab0fee3
7
tx.go
7
tx.go
@ -65,16 +65,13 @@ func (c *Tx) Process(ctx context.Context, cmd Cmder) error {
|
|||||||
// The transaction is automatically closed when fn exits.
|
// The transaction is automatically closed when fn exits.
|
||||||
func (c *Client) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
|
func (c *Client) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
|
||||||
tx := c.newTx(ctx)
|
tx := c.newTx(ctx)
|
||||||
|
defer tx.Close(ctx)
|
||||||
if len(keys) > 0 {
|
if len(keys) > 0 {
|
||||||
if err := tx.Watch(ctx, keys...).Err(); err != nil {
|
if err := tx.Watch(ctx, keys...).Err(); err != nil {
|
||||||
_ = tx.Close(ctx)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return fn(tx)
|
||||||
err := fn(tx)
|
|
||||||
_ = tx.Close(ctx)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the transaction, releasing any open resources.
|
// Close closes the transaction, releasing any open resources.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user