Merge branch 'reset-state-of-jedis-client' of github.com:HeartSaVioR/jedis into HeartSaVioR-reset-state-of-jedis-client
This commit is contained in:
@@ -29,15 +29,21 @@ public class BinaryClient extends Connection {
|
||||
}
|
||||
|
||||
private boolean isInMulti;
|
||||
|
||||
|
||||
private String password;
|
||||
|
||||
private long db;
|
||||
|
||||
private boolean isInWatch;
|
||||
|
||||
public boolean isInMulti() {
|
||||
return isInMulti;
|
||||
}
|
||||
|
||||
public boolean isInWatch() {
|
||||
return isInWatch;
|
||||
}
|
||||
|
||||
public BinaryClient(final String host) {
|
||||
super(host);
|
||||
}
|
||||
@@ -446,19 +452,23 @@ public class BinaryClient extends Connection {
|
||||
public void discard() {
|
||||
sendCommand(DISCARD);
|
||||
isInMulti = false;
|
||||
isInWatch = false;
|
||||
}
|
||||
|
||||
public void exec() {
|
||||
sendCommand(EXEC);
|
||||
isInMulti = false;
|
||||
isInWatch = false;
|
||||
}
|
||||
|
||||
public void watch(final byte[]... keys) {
|
||||
sendCommand(WATCH, keys);
|
||||
isInWatch = true;
|
||||
}
|
||||
|
||||
public void unwatch() {
|
||||
sendCommand(UNWATCH);
|
||||
isInWatch = false;
|
||||
}
|
||||
|
||||
public void sort(final byte[] key) {
|
||||
@@ -911,6 +921,14 @@ public class BinaryClient extends Connection {
|
||||
db = 0;
|
||||
super.disconnect();
|
||||
}
|
||||
|
||||
public void resetState() {
|
||||
if (isInMulti())
|
||||
discard();
|
||||
|
||||
if (isInWatch())
|
||||
unwatch();
|
||||
}
|
||||
|
||||
private void sendEvalCommand(Command command, byte[] script,
|
||||
byte[] keyCount, byte[][] params) {
|
||||
|
||||
@@ -1709,6 +1709,11 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKey
|
||||
public void disconnect() {
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
public void resetState() {
|
||||
client.resetState();
|
||||
client.getAll();
|
||||
}
|
||||
|
||||
public String watch(final byte[]... keys) {
|
||||
client.watch(keys);
|
||||
|
||||
@@ -84,6 +84,7 @@ public class JedisPool extends Pool<Jedis> {
|
||||
}
|
||||
|
||||
public void returnResource(final Jedis resource) {
|
||||
resource.resetState();
|
||||
returnResourceObject(resource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
||||
}
|
||||
|
||||
public void returnResource(final Jedis resource) {
|
||||
resource.resetState();
|
||||
returnResourceObject(resource);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.Transaction;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
public class JedisPoolTest extends Assert {
|
||||
@@ -176,4 +177,25 @@ public class JedisPoolTest extends Assert {
|
||||
pool0.returnResource(jedis);
|
||||
pool0.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnResourceShouldResetState() {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMaxTotal(1);
|
||||
config.setBlockWhenExhausted(false);
|
||||
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(),
|
||||
2000, "foobared");
|
||||
|
||||
Jedis jedis = pool.getResource();
|
||||
jedis.set("hello", "jedis");
|
||||
Transaction t = jedis.multi();
|
||||
t.set("hello", "world");
|
||||
pool.returnResource(jedis);
|
||||
|
||||
Jedis jedis2 = pool.getResource();
|
||||
assertTrue(jedis == jedis2);
|
||||
assertEquals("jedis", jedis2.get("hello"));
|
||||
pool.returnResource(jedis2);
|
||||
pool.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,10 @@ import org.junit.Test;
|
||||
import redis.clients.jedis.DebugParams;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
import redis.clients.jedis.JedisSentinelPool;
|
||||
import redis.clients.jedis.Transaction;
|
||||
|
||||
public class JedisSentinelPoolTest extends JedisTestBase {
|
||||
private static final String MASTER_NAME = "mymaster";
|
||||
@@ -150,4 +152,24 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnResourceShouldResetState() {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMaxTotal(1);
|
||||
config.setBlockWhenExhausted(false);
|
||||
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
|
||||
config, 1000, "foobared", 2);
|
||||
|
||||
Jedis jedis = pool.getResource();
|
||||
jedis.set("hello", "jedis");
|
||||
Transaction t = jedis.multi();
|
||||
t.set("hello", "world");
|
||||
pool.returnResource(jedis);
|
||||
|
||||
Jedis jedis2 = pool.getResource();
|
||||
assertTrue(jedis == jedis2);
|
||||
assertEquals("jedis", jedis2.get("hello"));
|
||||
pool.returnResource(jedis2);
|
||||
pool.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,12 @@ import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Protocol.Keyword;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
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 };
|
||||
@@ -294,4 +296,48 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertNull(results);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResetStateWhenInMulti() {
|
||||
jedis.auth("foobared");
|
||||
|
||||
Transaction t = jedis.multi();
|
||||
t.set("foooo", "barrr");
|
||||
|
||||
jedis.resetState();
|
||||
assertEquals(null, jedis.get("foooo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResetStateWhenInMultiWithinPipeline() {
|
||||
jedis.auth("foobared");
|
||||
|
||||
Pipeline p = jedis.pipelined();
|
||||
p.multi();
|
||||
p.set("foooo", "barrr");
|
||||
|
||||
jedis.resetState();
|
||||
assertEquals(null, jedis.get("foooo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResetStateWhenInWatch() {
|
||||
jedis.watch("mykey", "somekey");
|
||||
|
||||
// state reset : unwatch
|
||||
jedis.resetState();
|
||||
|
||||
Transaction t = jedis.multi();
|
||||
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set("mykey", "bar");
|
||||
nj.disconnect();
|
||||
|
||||
t.set("mykey", "foo");
|
||||
List<Object> resp = t.exec();
|
||||
assertNotNull(resp);
|
||||
assertEquals(1, resp.size());
|
||||
assertEquals("foo", jedis.get("mykey"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user