当前使用的redis版本
#redis-cli -v redis-cli 2.6.4
redis 192.168.1.53:6379> multi OK redis 192.168.1.53:6379> incr foo QUEUED redis 192.168.1.53:6379> set t1 1 QUEUED redis 192.168.1.53:6379> exec 1) (integer) 2 2) OK
Jedis jedis = new Jedis("192.168.1.53", 6379); Transaction tx = jedis.multi(); tx.incr( "foo"); tx.set( "t1", "2"); List<Object> result = tx.exec(); if (result == null || result.isEmpty()) { System. err.println( "Transaction error..."); return ; } for (Object rt : result) { System. out.println(rt.toString()); }
redis 192.168.1.53:6379> multi OK redis 192.168.1.53:6379> incr foo QUEUED redis 192.168.1.53:6379> set ff 11 22 (error) ERR wrong number of arguments for 'set' command redis 192.168.1.53:6379> exec 1) (integer) 4
+++++++++++命令+++++++++++
redis 192.168.1.53:6379> multi OK redis 192.168.1.53:6379> set a 11 QUEUED redis 192.168.1.53:6379> lpop a QUEUED redis 192.168.1.53:6379> exec 1) OK 2) (error) ERR Operation against a key holding the wrong kind of value+++++++++++对应的java代码 +++++++++++
Jedis jedis = new Jedis("192.168.1.53", 6379); Transaction tx = jedis.multi(); tx.set( "t1", "2"); tx.lpop( "t1"); List<Object> result = tx.exec(); if (result == null || result.isEmpty()) { System. err.println( "Transaction error..."); return ; } for (Object rt : result) { System. out.println(rt.toString()); }
redis 192.168.1.53:6379> set foo 1 OK redis 192.168.1.53:6379> multi OK redis 192.168.1.53:6379> incr foo QUEUED redis 192.168.1.53:6379> discard OK redis 192.168.1.53:6379> get foo "1"
redis 192.168.1.53:6379> watch foo OK redis 192.168.1.53:6379> set foo 5 OK redis 192.168.1.53:6379> multi OK redis 192.168.1.53:6379> set foo 9 QUEUED
+++++++++++暂停(执行完第二条命令才执行下面的)+++++++++++
redis 192.168.1.53:6379> exec (nil) redis 192.168.1.53:6379> get foo "8"
+++++++++++第二条命令+++++++++++
redis 192.168.1.53:6379> set foo 8 OK+++++++++++对应的java代码 +++++++++++
Jedis jedis = new Jedis("192.168.1.53", 6379); jedis.watch( "foo"); Transaction tx = jedis.multi(); tx.incr( "foo"); List<Object> result = tx.exec(); //运行时在这边打断点,然后通过命令行改变foo的值 if (result == null || result.isEmpty()) { System. err.println( "Transaction error..."); return; } for (Object rt : result) { System. out.println(rt.toString()); }