mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
parent
98d4ce4c09
commit
00e3ec32ca
@ -416,7 +416,22 @@ func (c *Connection) Execute(sql string, arguments ...interface{}) (commandTag s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Transaction(f func() bool) (committed bool, err error) {
|
func (c *Connection) Transaction(f func() bool) (committed bool, err error) {
|
||||||
if _, err = c.Execute("begin"); err != nil {
|
return c.transaction("", f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) TransactionIso(isoLevel string, f func() bool) (committed bool, err error) {
|
||||||
|
return c.transaction(isoLevel, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) transaction(isoLevel string, f func() bool) (committed bool, err error) {
|
||||||
|
var beginSql string
|
||||||
|
if isoLevel == "" {
|
||||||
|
beginSql = "begin"
|
||||||
|
} else {
|
||||||
|
beginSql = fmt.Sprintf("begin isolation level %s", isoLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = c.Execute(beginSql); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -536,3 +536,24 @@ func TestTransaction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTransactionIso(t *testing.T) {
|
||||||
|
conn, err := Connect(*defaultConnectionParameters)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
isoLevels := []string{"serializable", "repeatable read", "read committed", "read uncommitted"}
|
||||||
|
for _, iso := range isoLevels {
|
||||||
|
_, err := conn.TransactionIso(iso, func() bool {
|
||||||
|
if level := mustSelectValue(t, conn, "select current_setting('transaction_isolation')"); level != iso {
|
||||||
|
t.Errorf("Expected to be in isolation level %v but was %v", iso, level)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected transaction failure: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user