postgres: use stdlib to set serializable tx level

also use a context for the rollback, which is a bit cleaner since it
only results in one 'defer', rather than N from the loop
This commit is contained in:
Alex Suraci 2018-11-16 19:35:47 +00:00
parent 7e96021428
commit 9b9013a560

View file

@ -2,6 +2,7 @@
package sql
import (
"context"
"database/sql"
"regexp"
"time"
@ -51,15 +52,16 @@ var (
// NOTE(ericchiang): For some reason using `SET SESSION CHARACTERISTICS AS TRANSACTION` at a
// session level didn't work for some edge cases. Might be something worth exploring.
executeTx: func(db *sql.DB, fn func(sqlTx *sql.Tx) error) error {
for {
tx, err := db.Begin()
if err != nil {
return err
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
opts := &sql.TxOptions{
Isolation: sql.LevelSerializable,
}
defer tx.Rollback()
if _, err := tx.Exec(`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;`); err != nil {
for {
tx, err := db.BeginTx(ctx, opts)
if err != nil {
return err
}