-
Notifications
You must be signed in to change notification settings - Fork 13
Transaction
Tam. Nguyen Duc edited this page May 31, 2014
·
1 revision
Transaction is implemented using MULTI, EXEC and WATCH. Using transaction directly with a Conn is not goroutine-safe, so transaction should be used with connection pool only.
tr := gore.NewTransaction(conn)
tr.Watch("a key") // Watch a key
tr.Watch("another key")
rep, _ := NewCommand("GET", "a key").Run(conn)
value, _ := rep.Int()
tr.Add(NewCommand("SET", "a key", value + 1)) // Add a command to the transaction
_, err := tr.Commit() // Commit the transaction
if err == nil {
// Transaction OK!!!
} else if err == gore.ErrKeyChanged {
// Watched key has been changed, transaction should be started over.
} else {
// Other errors, transaction should be aborted
}