3 ms·
Bolt author here. The DB.View() and DB.Update() functions are simply wrappers around DB.Begin(). The View/Update functions were not part of the original API but
by benbjohnson 12y ago
Bolt author here. The DB.View() and DB.Update() functions are simply wrappers around DB.Begin(). The View/Update functions were not part of the original API but they're incredibly useful in practice and make the application code more idiomatic.
There are several benefits to using the wrapper functions:
1. It lets you return an error to rollback a read-write transaction. This is typically what you want. That error will get passed through as a return from DB.Update().
2. Because of #1, you can have one error check for application errors and system errors (e.g. disk full) after a transaction commits instead of an error check for DB.Begin() and DB.Commit().
3. If you forget to close a read transaction then it will keep old pages around to maintain consistency but this will cause your database to grow without bound. If you use the helper functions then you're protected from this even in the event of a panic().
The API is modeled around database/sql where applicable. Let me know if you find anything confusing and I can update the documentation to make it more clear.
http://godoc.org/github.com/boltdb/bolt#DB.Begin http://godoc.org/github.com/boltdb/bolt#DB.Begin
- akavel 12y agoIt would be nice if you mentioned the DB.Begin() etc. in the Readme, not only the Update() & friends, so that a casual reader would know he has a choice here. I couldn't find it mentioned there, so how could I know I have to search for it among dozens functions in godoc? Never really tried yet, but I think when using database/sql.Tx, the following snippet would be quite idiomatic, and I believe it should be perfectly safe: tx, err := mydb.Begin() if err != nil { return err } defer tx.Rollback() _, err = tx.Exec('...') if err != nil { return err } return tx.Commit()
- AYBABTME 12y agoYou'll be damned if you have to read the godocs of a DB you want to use. =P
- mrweasel 12y agoI would actually prefer to have the Begin, Commit in the readme and then a recommendation to use the Update and View. It might just be a difference in learning/understanding, but I understand the reasoning behind Update, View after reading the comments regarding the caviats of manually dealing with the transactions.
- benbjohnson 12y agoI just added it: https://github.com/boltdb/bolt#managing-transactions-manually https://github.com/boltdb/bolt#managing-transactions-manuall...
- benbjohnson 12y agoThanks for the suggestion. I added it to the README (33e7a07): https://github.com/boltdb/bolt#managing-transactions-manually https://github.com/boltdb/bolt#managing-transactions-manuall... The "defer tx.Rollback()" makes me cringe a little since it's confusing when you read the code top-down. It also throws out the error returned from Rollback(). Granted, the only possible error is if the DB is closed so it's not a big deal in most situations.
- AYBABTME 12y agoHow painful does this look? func action(...) (err error) { tx, err := db.Begin() if err != nil { return fmt.Errorf("preparing transaction: %v", err) } defer func(tx *sql.Tx) { if err != nil { _ = tx.Rollback() } else { err = tx.Commit() } }(tx) // do something return }