2013-04-18 119 views
0

我正在學習jedis,我無法準確找出這段代碼中的問題。任何人都可以幫助我。異常發生在聲明tx.exec()ERR EXEC without MULTI - Jedis

public class JedisFactory { 

public static void main (String [] args){ 
    JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379); 
    Jedis jedis = pool.getResource(); 

    Pipeline pipeline = jedis.pipelined(); 
    for(int i=0; i < 1000 ; i++){ 
     pipeline.hincrBy("Id", i+"", i); 
    } 
    pipeline.exec();   
    pool.returnResource(jedis); 

    jedis = pool.getResource(); 
    Transaction tx = jedis.multi(); 
    Response<Map<String,String>> map = tx.hgetAll("Id"); 
    tx.hincrBy("Id","2", 1); 
    **tx.exec();** 
    //Map<String,String> map1 = jedis.hgetAll("Id"); 

    pool.returnResource(jedis); 
    pool.destroy(); 
} 
} 
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR EXEC without MULTI 
    at redis.clients.jedis.Protocol.processError(Protocol.java:54) 
    at redis.clients.jedis.Protocol.process(Protocol.java:61) 
    at redis.clients.jedis.Protocol.read(Protocol.java:122) 
    at redis.clients.jedis.Connection.getAll(Connection.java:207) 
    at redis.clients.jedis.BinaryTransaction.exec(BinaryTransaction.java:23) 
    at com.work.JedisFactory.main(JedisFactory.java:30) 

回答

1

在for循環之前添加pipeline.multi()解決了問題。但修復之前在其他一些行中拋出的異常。

0

我想你應該使用pipeline.execute()而不是pipeline.exec(),至少我是這樣使用它,並且沒問題。

+2

Pipeline中沒有叫做execute()的方法 - Jedis 2.1.0 – user1182253