Rollback in progress transactions when releasing connection

pgx-vs-pq
Jack Christensen 2013-07-13 09:15:40 -04:00
parent e162a0bbcb
commit ad27e43224
2 changed files with 40 additions and 0 deletions

View File

@ -33,6 +33,9 @@ func (p *ConnectionPool) Acquire() (c *Connection) {
}
func (p *ConnectionPool) Release(c *Connection) {
if c.txStatus != 'I' {
c.Execute("rollback")
}
p.connectionChannel <- c
}

View File

@ -108,3 +108,40 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) {
pool.Release(c)
}
}
func TestPoolReleaseWithTransactions(t *testing.T) {
pool := createConnectionPool(1)
defer pool.Close()
var err error
conn := pool.Acquire()
if _, err = conn.Execute("begin"); err != nil {
t.Fatalf("Unexpected error begining transaction: %v", err)
}
if _, err = conn.Execute("select"); err == nil {
t.Fatal("Did not receive expected error")
}
if conn.txStatus != 'E' {
t.Fatalf("Expected txStatus to be 'E', instead it was '%c'", conn.txStatus)
}
pool.Release(conn)
if conn.txStatus != 'I' {
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.txStatus)
}
conn = pool.Acquire()
if _, err = conn.Execute("begin"); err != nil {
t.Fatalf("Unexpected error begining transaction: %v", err)
}
if conn.txStatus != 'T' {
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.txStatus)
}
pool.Release(conn)
if conn.txStatus != 'I' {
t.Fatalf("Expected release to rollback uncommitted transaction, but it did not: '%c'", conn.txStatus)
}
}