Jedis事务:Cannot use Jedis when in Multi. Please use Transaction or reset jedis state.

执行程序:

@Test
    void contextLoads() {
        Jedis jedis = new Jedis( "47.100.127.116",30000);
        
				Transaction multi = jedis.multi();
      	jedis.flushDB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","word");
        jsonObject.put("name","lingg");
        String result = jsonObject.toJSONString();
        
        try {
            multi .set("user1",result);
            multi.set("user2",result);
            multi.get("qwe");
            multi.exec();
        } catch (Exception e) {
            multi.discard();
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }
    }

Jedis开启事务后报错:

redis.clients.jedis.exceptions.JedisDataException: Cannot use Jedis when in Multi. Please use Transaction or reset jedis state.

原因:

Jedis 开启了事务后就不能再用Jedis的实例jedis进行操作了,知道事务关闭前都只能用开启事务时间返回的实例(上述例子中为multi)进行操作,否则会抛出上述异常

解决:
将事务开启后 到 事务结束前 这个范围内 所有用jedis进行的操作移到这个范围之外进行。

上一篇:git拉代码时报错error: RPC failed; curl 56 Recv failure: Connection reset by peer


下一篇:异步复位,同步释放