說明:
使用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));
問題解決。