forked from mystiq/dex
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:
parent
7e96021428
commit
9b9013a560
1 changed files with 9 additions and 7 deletions
|
@ -2,6 +2,7 @@
|
||||||
package sql
|
package sql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
@ -51,18 +52,19 @@ 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 {
|
||||||
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
opts := &sql.TxOptions{
|
||||||
|
Isolation: sql.LevelSerializable,
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
tx, err := db.Begin()
|
tx, err := db.BeginTx(ctx, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer tx.Rollback()
|
|
||||||
|
|
||||||
if _, err := tx.Exec(`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;`); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := fn(tx); err != nil {
|
if err := fn(tx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue