Created
July 24, 2018 06:11
-
-
Save ants/a91ab3b362e6504789f3528cf25910ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In main part of the application | |
db, err := sql.Open("postgres", connectString) | |
defer db.Close() | |
checkErr(err) | |
// In single request | |
tx, err := db.BeginTx(context.TODO(), TxOptions{sql.LevelRepeatableRead}) // Gets a single connection from pool and start transaction | |
checkErr(err) | |
rows, err := tx.Query(sql1) // Run query in that transaction | |
checkErr(err) | |
doStuffWith(rows) | |
tx.Query(sql2) | |
checkErr(err) | |
doStuffWith(rows) | |
tx.Commit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment