3 ms·
It 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
by akavel 12y ago
It 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 }