Skip to content

Commit ce8d932

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-226 - Syntax exceptions using lettuce should be reported immediately.
If running in transaction mode we now do a rudimentary check if the provided number of arguments matches the ones defined for the RedisCommand to execute. If the command passes this first check and causes an error on redis server one has to check results for Command.getOutput().hasError(). Original Pull Request: #66.
1 parent bc1bbd1 commit ce8d932

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.data.redis.connection.Subscription;
5050
import org.springframework.data.redis.connection.convert.Converters;
5151
import org.springframework.data.redis.connection.convert.TransactionResultConverter;
52+
import org.springframework.data.redis.core.RedisCommand;
5253
import org.springframework.data.redis.core.types.RedisClientInfo;
5354
import org.springframework.util.Assert;
5455
import org.springframework.util.ClassUtils;
@@ -294,18 +295,22 @@ public Object execute(String command, CommandOutput commandOutputTypeHint, byte[
294295
try {
295296
String name = command.trim().toUpperCase();
296297
CommandType cmd = CommandType.valueOf(name);
297-
CommandArgs<byte[], byte[]> cmdArg = new CommandArgs<byte[], byte[]>(CODEC);
298298

299+
validateCommandIfRunningInTransactionMode(cmd, args);
300+
301+
CommandArgs<byte[], byte[]> cmdArg = new CommandArgs<byte[], byte[]>(CODEC);
299302
if (!ObjectUtils.isEmpty(args)) {
300303
cmdArg.addKeys(args);
301304
}
302305

303306
CommandOutput expectedOutput = commandOutputTypeHint != null ? commandOutputTypeHint : typeHints.getTypeHint(cmd);
304307
if (isPipelined()) {
308+
305309
pipeline(new LettuceResult(getAsyncConnection().dispatch(cmd, expectedOutput, cmdArg)));
306310
return null;
307311
} else if (isQueueing()) {
308-
transaction(new LettuceResult(getAsyncConnection().dispatch(cmd, expectedOutput, cmdArg)));
312+
313+
transaction(new LettuceTxResult(getAsyncConnection().dispatch(cmd, expectedOutput, cmdArg)));
309314
return null;
310315
} else {
311316
return await(getAsyncConnection().dispatch(cmd, expectedOutput, cmdArg));
@@ -3168,6 +3173,25 @@ private ZStoreArgs zStoreArgs(Aggregate aggregate, int[] weights) {
31683173
return args;
31693174
}
31703175

3176+
private void validateCommandIfRunningInTransactionMode(CommandType cmd, byte[]... args) {
3177+
3178+
if (this.isQueueing()) {
3179+
validateCommand(cmd, args);
3180+
}
3181+
}
3182+
3183+
private void validateCommand(CommandType cmd, byte[]... args) {
3184+
3185+
RedisCommand redisCommand = RedisCommand.failsafeCommandLookup(cmd.name());
3186+
if (!RedisCommand.UNKNOWN.equals(redisCommand) && redisCommand.requiresArguments()) {
3187+
try {
3188+
redisCommand.validateArgumentCount(args != null ? args.length : 0);
3189+
} catch (IllegalArgumentException e) {
3190+
throw new InvalidDataAccessApiUsageException(String.format("Validation failed for %s command.", cmd), e);
3191+
}
3192+
}
3193+
}
3194+
31713195
/**
31723196
* {@link TypeHints} provide {@link CommandOutput} information for a given {@link CommandType}.
31733197
*

src/main/java/org/springframework/data/redis/core/RedisCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @see Redis command list:
3333
* https://github.com/antirez/redis/blob/93e7a130fc9594e41ccfc996b5eca7626ae5356a/src/redis.c#L119
3434
*/
35-
enum RedisCommand {
35+
public enum RedisCommand {
3636
// -- A
3737
APPEND("rw", 2, 2), //
3838
AUTH("rw", 1, 1), //

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionTransactionIntegrationTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.util.Arrays;
2121

22-
import org.junit.Ignore;
2322
import org.junit.Test;
2423
import org.junit.runner.RunWith;
2524
import org.springframework.data.redis.connection.AbstractConnectionTransactionIntegrationTests;
@@ -40,10 +39,6 @@
4039
@ContextConfiguration("LettuceConnectionIntegrationTests-context.xml")
4140
public class LettuceConnectionTransactionIntegrationTests extends AbstractConnectionTransactionIntegrationTests {
4241

43-
@Test
44-
@Ignore("DATAREDIS-226 Exceptions on native execute are swallowed in tx")
45-
public void exceptionExecuteNative() throws Exception {}
46-
4742
@Test(expected = UnsupportedOperationException.class)
4843
@IfProfileValue(name = "redisVersion", value = "2.6")
4944
public void testSRandMemberCountNegative() {

0 commit comments

Comments
 (0)