BinaryJedis.multi(TransactionBlock) should not call discard when exception occurred
* In BinaryJedis.multi(TransactionBlock), multi & exec already fired before exception occured, so sending discard has no effect, and made another error ** add unit test (error inside TransactionBlock) *** Transaction with error - Redis discards transaction automatically (execabort) *** Transaction with error - Redis doesn't roll back (force to execute all)
This commit is contained in:
@@ -1685,13 +1685,9 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKey
|
|||||||
public List<Object> multi(final TransactionBlock jedisTransaction) {
|
public List<Object> multi(final TransactionBlock jedisTransaction) {
|
||||||
List<Object> results = null;
|
List<Object> results = null;
|
||||||
jedisTransaction.setClient(client);
|
jedisTransaction.setClient(client);
|
||||||
try {
|
client.multi();
|
||||||
client.multi();
|
jedisTransaction.execute();
|
||||||
jedisTransaction.execute();
|
results = jedisTransaction.exec();
|
||||||
results = jedisTransaction.exec();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
jedisTransaction.discard();
|
|
||||||
}
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ import redis.clients.jedis.Response;
|
|||||||
import redis.clients.jedis.Transaction;
|
import redis.clients.jedis.Transaction;
|
||||||
import redis.clients.jedis.TransactionBlock;
|
import redis.clients.jedis.TransactionBlock;
|
||||||
import redis.clients.jedis.exceptions.JedisDataException;
|
import redis.clients.jedis.exceptions.JedisDataException;
|
||||||
|
import redis.clients.jedis.exceptions.JedisException;
|
||||||
|
|
||||||
public class TransactionCommandsTest extends JedisCommandTestBase {
|
public class TransactionCommandsTest extends JedisCommandTestBase {
|
||||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||||
@@ -105,6 +107,50 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multiBlockWithErrorRedisDiscardsTransaction() throws Exception {
|
||||||
|
// Transaction with error - Redis discards transaction automatically (Syntax Error, etc.)
|
||||||
|
TransactionBlock tb = new TransactionBlock() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() throws JedisException {
|
||||||
|
del("hello");
|
||||||
|
hmset("hello", new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
jedis.multi(tb);
|
||||||
|
} catch (JedisDataException e) {
|
||||||
|
assertTrue(e.getMessage().contains("EXECABORT"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multiBlockWithErrorRedisForceToExecuteAllCommands() throws Exception {
|
||||||
|
// Transaction with error - Redis doesn't roll back (Type Error, Deletion of non-exist key, etc.)
|
||||||
|
jedis.del("hello2");
|
||||||
|
TransactionBlock tb2 = new TransactionBlock() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() throws JedisException {
|
||||||
|
del("hello2");
|
||||||
|
set("hello2", "hello");
|
||||||
|
sadd("hello2", "hello2");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Object> responses = jedis.multi(tb2);
|
||||||
|
assertEquals("OK", responses.get(1));
|
||||||
|
assertEquals(JedisDataException.class, responses.get(2).getClass());
|
||||||
|
|
||||||
|
Exception exc = (JedisDataException) responses.get(2);
|
||||||
|
assertTrue(exc.getMessage().contains("WRONGTYPE"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void watch() throws UnknownHostException, IOException {
|
public void watch() throws UnknownHostException, IOException {
|
||||||
jedis.watch("mykey", "somekey");
|
jedis.watch("mykey", "somekey");
|
||||||
@@ -294,4 +340,5 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
|
|||||||
|
|
||||||
assertNull(results);
|
assertNull(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user