Watch, Transactions

637 views
Skip to first unread message

Alex Black

unread,
May 11, 2011, 2:18:05 PM5/11/11
to Jedis
Hi, I'm using watch for the first time. My goal is to implement
something like check-and-set (CAS) behavior (http://redis.io/topics/
transactions)

So I've got code (its Scala, not Java) like this:

jedis.watch(key)
val value: String = jedis.get(key)

// do calculations
val newValue: String = calculate(value)

val t: Transaction = jedis.multi
t.set(key, newValue)

t.exec

So, how what I want to have happen is to fail/abort the transaction if
key was modified in between my get and set.
1. Is this correct?
2. How do I detect the failure scenario? Maybe t.exec returns null
when the transaction is aborted?

thanks

- Alex

Jonathan Leibiusky

unread,
May 11, 2011, 2:48:07 PM5/11/11
to jedis...@googlegroups.com
Hi!
I like scala a lot! another language to learn ASAP!

On Wed, May 11, 2011 at 3:18 PM, Alex Black <al...@alexblack.ca> wrote:
Hi, I'm using watch for the first time. My goal is to implement
something like check-and-set (CAS) behavior (http://redis.io/topics/
transactions)

So I've got code (its Scala, not Java) like this:

jedis.watch(key)
val value: String = jedis.get(key)

// do calculations
val newValue: String = calculate(value)

val t: Transaction = jedis.multi
t.set(key, newValue)

t.exec

So, how what I want to have happen is to fail/abort the transaction if
key was modified in between my get and set.
1. Is this correct?

yes, it seems like it is correct. you can test it easily by sleeping between the get and the set and from another thread change the value :)
that is how tests are done in jedis btw.
 
2. How do I detect the failure scenario? Maybe t.exec returns null
when the transaction is aborted?

exactly, you will get null from the exec, that means that nothing was done in the multi/exec block, so it means your transaction failed.
 

thanks

- Alex

Alex Black

unread,
May 11, 2011, 3:22:35 PM5/11/11
to Jedis
Thanks!
Reply all
Reply to author
Forward
0 new messages