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 package sql
import ( import (
"context"
"database/sql" "database/sql"
"regexp" "regexp"
"time" "time"
@ -51,15 +52,16 @@ var (
// NOTE(ericchiang): For some reason using `SET SESSION CHARACTERISTICS AS TRANSACTION` at a // 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. // 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 { executeTx: func(db *sql.DB, fn func(sqlTx *sql.Tx) error) error {
for { ctx, cancel := context.WithCancel(context.TODO())
tx, err := db.Begin() defer cancel()
if err != nil {
return err opts := &sql.TxOptions{
Isolation: sql.LevelSerializable,
} }
defer tx.Rollback() for {
tx, err := db.BeginTx(ctx, opts)
if _, err := tx.Exec(`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;`); err != nil { if err != nil {
return err return err
} }