diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-07-11 09:54:10 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-07-11 09:54:10 -0600 |
commit | ac4838472da343493c54c7649deb3b20669f8c3b (patch) | |
tree | a0da15376d9bd4b54877487e6f9c5d3614bd0d59 /db.go | |
parent | Merge pull request #219 from benbjohnson/freelist-overflow (diff) | |
download | dedo-ac4838472da343493c54c7649deb3b20669f8c3b.tar.gz dedo-ac4838472da343493c54c7649deb3b20669f8c3b.tar.xz |
Recover from panics appropriately.
This commit adds a defer handler to ensure that transactions are always closed out - even
in the event of a panic within user code. It's recommended that applications always fail
in the event of a panic but some packages such as net/http will automatically recover
which is a problem (IHMO).
Diffstat (limited to 'db.go')
-rw-r--r-- | db.go | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -440,6 +440,13 @@ func (db *DB) Update(fn func(*Tx) error) error { return err } + // Make sure the transaction rolls back in the event of a panic. + defer func() { + if t.db != nil { + t.rollback() + } + }() + // Mark as a managed tx so that the inner function cannot manually commit. t.managed = true @@ -464,6 +471,13 @@ func (db *DB) View(fn func(*Tx) error) error { return err } + // Make sure the transaction rolls back in the event of a panic. + defer func() { + if t.db != nil { + t.rollback() + } + }() + // Mark as a managed tx so that the inner function cannot manually rollback. t.managed = true |