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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue