Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how RunInTransaction works #140

Closed
viblo opened this issue Mar 24, 2016 · 1 comment
Closed

Document how RunInTransaction works #140

viblo opened this issue Mar 24, 2016 · 1 comment

Comments

@viblo
Copy link

viblo commented Mar 24, 2016

We have a small go coding test for potential new employees at my company. From what I can tell a lot of our candidates dont understand how RunInTransaction works. The "tricky" part is that you have to actually use the inner pg.Tx to make use of the transaction which many of them doesnt seem to get. Now since this is a coding test it doesnt have any critical effect except it makes it easy for us to filter them out, but I suspect that a lot of normal users of go-pg will make the same mistake.

What Ive seen people do is this:

db.RunInTransaction(func(tx *pg.Tx) error {
    db.Exec("insert into users (a) values(1)")
    tx.Rollback()
    return nil
})

Instead you should of course do it this way if you want the insert to be rollbacked:

db.RunInTransaction(func(tx *pg.Tx) error {
    tx.Exec("insert into users (a) values(1)")
    tx.Rollback()
    return nil
})

Maybe you can add an example / clarify the documentation of this function? I think that can save a lot of tricky debugging for unexperienced users of this library.

@vmihailenco
Copy link
Member

Thanks for feedback, added https://godoc.org/gopkg.in/pg.v4#example-DB-RunInTransaction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants