springboot-data-redis 遇到ERR EXEC without MULTI


说明:

使用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));

 

问题解决。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM