JedisPool / JedisSentinelPool resets returning object's state (watched,

multi)

* BinaryClient / BinaryJedis : added feature to reset its state
(watched, multi)
* JedisPool / JedisSentinelPool : calls new feature (reset state) when
Jedis object returns to pool
* Unit Test included
This commit is contained in:
Jungtaek Lim
2013-12-21 01:33:46 +09:00
parent 46734e646a
commit 3073f778b4
7 changed files with 116 additions and 1 deletions

View File

@@ -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);
}
@@ -447,19 +453,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) {
@@ -912,6 +922,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) {

View File

@@ -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);

View File

@@ -84,6 +84,7 @@ public class JedisPool extends Pool<Jedis> {
}
public void returnResource(final Jedis resource) {
resource.resetState();
returnResourceObject(resource);
}
}

View File

@@ -79,6 +79,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
}
public void returnResource(final Jedis resource) {
resource.resetState();
returnResourceObject(resource);
}