Skip to content

Instantly share code, notes, and snippets.

@ants
Created July 24, 2018 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ants/a91ab3b362e6504789f3528cf25910ca to your computer and use it in GitHub Desktop.
Save ants/a91ab3b362e6504789f3528cf25910ca to your computer and use it in GitHub Desktop.
// 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