From b8486d6749f293951a5586a0a122fbf0787cf2b5 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 24 Mar 2016 15:11:04 -0500 Subject: [PATCH] Fix Tx.status not being set on error on Commit fixes #131 --- CHANGELOG.md | 1 + tx.go | 1 + tx_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae390daa..3eaf5d87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ## Fixes * Fix compilation on 32-bit architecture +* Fix Tx.status not being set on error on Commit # 2.8.0 (March 18, 2016) diff --git a/tx.go b/tx.go index 4183f403..292148a4 100644 --- a/tx.go +++ b/tx.go @@ -86,6 +86,7 @@ func (tx *Tx) Commit() error { tx.status = TxStatusCommitFailure tx.err = ErrTxCommitRollback } else { + tx.status = TxStatusCommitFailure tx.err = err } diff --git a/tx_test.go b/tx_test.go index c0019970..435521a3 100644 --- a/tx_test.go +++ b/tx_test.go @@ -94,6 +94,52 @@ func TestTxCommitWhenTxBroken(t *testing.T) { } } +func TestTxCommitSerializationFailure(t *testing.T) { + t.Parallel() + + pool := createConnPool(t, 5) + defer pool.Close() + + pool.Exec(`drop table if exists tx_serializable_sums`) + _, err := pool.Exec(`create table tx_serializable_sums(num integer);`) + if err != nil { + t.Fatalf("Unable to create temporary table: %v", err) + } + defer pool.Exec(`drop table tx_serializable_sums`) + + tx1, err := pool.BeginIso(pgx.Serializable) + if err != nil { + t.Fatalf("BeginIso failed: %v", err) + } + defer tx1.Rollback() + + tx2, err := pool.BeginIso(pgx.Serializable) + if err != nil { + t.Fatalf("BeginIso failed: %v", err) + } + defer tx2.Rollback() + + _, err = tx1.Exec(`insert into tx_serializable_sums(num) select sum(num) from tx_serializable_sums`) + if err != nil { + t.Fatalf("Exec failed: %v", err) + } + + _, err = tx2.Exec(`insert into tx_serializable_sums(num) select sum(num) from tx_serializable_sums`) + if err != nil { + t.Fatalf("Exec failed: %v", err) + } + + err = tx1.Commit() + if err != nil { + t.Fatalf("Commit failed: %v", err) + } + + err = tx2.Commit() + if pgErr, ok := err.(pgx.PgError); !ok || pgErr.Code != "40001" { + t.Fatalf("Expected serialization error 40001, got %#v", err) + } +} + func TestTransactionSuccessfulRollback(t *testing.T) { t.Parallel()