Commit 9b9013a5 authored by Alex Suraci's avatar Alex Suraci

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
parent 7e960214
......@@ -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()
defer tx.Rollback()
opts := &sql.TxOptions{
Isolation: sql.LevelSerializable,
}
if _, err := tx.Exec(`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;`); err != nil {
for {
tx, err := db.BeginTx(ctx, opts)
if err != nil {
return err
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment