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