<!-- language: lang-java -->
Object result = stringRedisTemplate.execute(new SessionCallback<Session>() {
public Session execute(RedisOperations redisOperations) throws DataAccessException {
try{
redisOperations.watch(xxx);
redisOperations.multi();
int i = 1/0; //Test redis transaction rollback
//...
List<Object> execResult = redisOperations.exec();
}finally{
redisOperations.unwatch();
return null
}
}
在multi和exec事务代码块的中间,故意添加一行(int i = 1/0
)将引发异常代码以测试SDR事务回滚,并发现异常(redis.clients.jedis.exceptions.JedisDataException: Cannot use Jedis when in Multi. Please use Transation or reset jedis state.
)被抛出。
为什么会引发此异常?