walletdb: ensure transactions are rolled back on panic
This commit is contained in:
parent
e7e46cd6f9
commit
da2fe0e3b0
1 changed files with 18 additions and 0 deletions
|
@ -210,11 +210,20 @@ func View(db DB, f func(tx ReadTx) error) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make sure the transaction rolls back in the event of a panic.
|
||||
defer func() {
|
||||
if tx != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
err = f(tx)
|
||||
rollbackErr := tx.Rollback()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rollbackErr != nil {
|
||||
return rollbackErr
|
||||
}
|
||||
|
@ -232,6 +241,14 @@ func Update(db DB, f func(tx ReadWriteTx) error) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make sure the transaction rolls back in the event of a panic.
|
||||
defer func() {
|
||||
if tx != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
err = f(tx)
|
||||
if err != nil {
|
||||
// Want to return the original error, not a rollback error if
|
||||
|
@ -239,6 +256,7 @@ func Update(db DB, f func(tx ReadWriteTx) error) error {
|
|||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue