Skip to content

Commit

Permalink
Cleanup MULTI state if MULTI command fails #1650
Browse files Browse the repository at this point in the history
We now clear the MULTI state on a best-effort basis when the MULTI command fails (Redis error, timeout, …).
  • Loading branch information
mp911de committed Mar 11, 2021
1 parent d75d990 commit 8adefb0
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/main/java/io/lettuce/core/StatefulRedisConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ public <T> RedisCommand<K, V, T> dispatch(RedisCommand<K, V, T> command) {

RedisCommand<K, V, T> toSend = preProcessCommand(command);

if (command.getType().name().equals(MULTI.name())) {
multi = (multi == null ? new MultiOutput<>(codec) : multi);
}
potentiallyEnableMulti(command);

return super.dispatch(toSend);
}
Expand All @@ -174,14 +172,28 @@ public <T> RedisCommand<K, V, T> dispatch(RedisCommand<K, V, T> command) {
RedisCommand<K, V, ?> command = preProcessCommand(o);

sentCommands.add(command);
if (command.getType().name().equals(MULTI.name())) {
multi = (multi == null ? new MultiOutput<>(codec) : multi);
}
potentiallyEnableMulti(command);
});

return super.dispatch(sentCommands);
}

private void potentiallyEnableMulti(RedisCommand<K, V, ?> command) {

if (command.getType().name().equals(MULTI.name())) {

multi = (multi == null ? new MultiOutput<>(codec) : multi);

if (command instanceof CompleteableCommand) {
((CompleteableCommand<?>) command).onComplete((ignored, e) -> {
if (e != null) {
multi = null;
}
});
}
}
}

protected <T> RedisCommand<K, V, T> preProcessCommand(RedisCommand<K, V, T> command) {

RedisCommand<K, V, T> local = command;
Expand Down

0 comments on commit 8adefb0

Please sign in to comment.