首页 > 技术文章 > springboot-data-redis 遇到ERR EXEC without MULTI

leite 2021-08-09 22:48 原文

说明:

使用springboot的redistemplate接口执行事务

        redisTemplate.multi();
        //此处为业务逻辑
        redisTemplate.exec();

 

时,遇到错误:ERR EXEC without MULTI.

 

解决:

查阅官方文档发现,redistemplate不支持这样的写法。需要改成:

//execute a transaction
List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
  public List<Object> execute(RedisOperations operations) throws DataAccessException {
    operations.multi();
    operations.opsForSet().add("key", "value1");

    // This will contain the results of all operations in the transaction
    return operations.exec();
  }
});
System.out.println("Number of items added to set: " + txResults.get(0));

 

问题解决。

 

推荐阅读