Merge branch 'transaction-block-should-not-call-discard-when-exception-ocurred' of github.com:HeartSaVioR/jedis into HeartSaVioR-transaction-block-should-not-call-discard-when-exception-ocurred
Conflicts: src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java
This commit is contained in:
@@ -1699,13 +1699,9 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
|
||||
public List<Object> multi(final TransactionBlock jedisTransaction) {
|
||||
List<Object> results = null;
|
||||
jedisTransaction.setClient(client);
|
||||
try {
|
||||
client.multi();
|
||||
jedisTransaction.execute();
|
||||
results = jedisTransaction.exec();
|
||||
} catch (Exception ex) {
|
||||
jedisTransaction.discard();
|
||||
}
|
||||
client.multi();
|
||||
jedisTransaction.execute();
|
||||
results = jedisTransaction.exec();
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -17,6 +18,7 @@ import redis.clients.jedis.Response;
|
||||
import redis.clients.jedis.Transaction;
|
||||
import redis.clients.jedis.TransactionBlock;
|
||||
import redis.clients.jedis.exceptions.JedisDataException;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
public class TransactionCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
@@ -106,6 +108,52 @@ 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
|
||||
public void watch() throws UnknownHostException, IOException {
|
||||
jedis.watch("mykey", "somekey");
|
||||
|
||||
Reference in New Issue
Block a user