Fix Tx.status not being set on error on Commit

fixes #131
pull/133/head
Jack Christensen 2016-03-24 15:11:04 -05:00
parent 7947ab561f
commit b8486d6749
3 changed files with 48 additions and 0 deletions

View File

@ -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)

1
tx.go
View File

@ -86,6 +86,7 @@ func (tx *Tx) Commit() error {
tx.status = TxStatusCommitFailure
tx.err = ErrTxCommitRollback
} else {
tx.status = TxStatusCommitFailure
tx.err = err
}

View File

@ -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()