Skip to content

Commit

Permalink
Merge pull request redis#145 from TioBorracho/Cleaning
Browse files Browse the repository at this point in the history
When there is a connection error, disconnect to leave everything clean.
  • Loading branch information
xetorthio committed May 16, 2011
2 parents 6a78b32 + 5c9d516 commit 4bc292e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
.gradle/
target/
build/
/appendonly.aof
/dump.rdb
/nohup.out
38 changes: 28 additions & 10 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ protected void flush() {
}
}

protected Object read() {
try {
return protocol.read(inputStream);
} catch (JedisConnectionException e) {
disconnect();
throw new JedisConnectionException(e);
}
}

protected void sendProtocolCommand(final Command cmd, final byte[]... args) {
try {
protocol.sendCommand(outputStream, cmd, args);
} catch (JedisConnectionException e) {
disconnect();
throw new JedisConnectionException(e);
}
}

protected Connection sendCommand(final Command cmd, final String... args) {
final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < args.length; i++) {
Expand All @@ -71,14 +89,14 @@ protected Connection sendCommand(final Command cmd, final String... args) {

protected Connection sendCommand(final Command cmd, final byte[]... args) {
connect();
protocol.sendCommand(outputStream, cmd, args);
sendProtocolCommand(cmd, args);
pipelinedCommands++;
return this;
}

protected Connection sendCommand(final Command cmd) {
connect();
protocol.sendCommand(outputStream, cmd, new byte[0][]);
sendProtocolCommand(cmd, new byte[0][]);
pipelinedCommands++;
return this;
}
Expand Down Expand Up @@ -145,7 +163,7 @@ public boolean isConnected() {
protected String getStatusCodeReply() {
flush();
pipelinedCommands--;
final byte[] resp = (byte[]) protocol.read(inputStream);
final byte[] resp = (byte[]) read();
if (null == resp) {
return null;
} else {
Expand All @@ -165,13 +183,13 @@ public String getBulkReply() {
public byte[] getBinaryBulkReply() {
flush();
pipelinedCommands--;
return (byte[]) protocol.read(inputStream);
return (byte[]) read();
}

public Long getIntegerReply() {
flush();
pipelinedCommands--;
return (Long) protocol.read(inputStream);
return (Long) read();
}

public List<String> getMultiBulkReply() {
Expand All @@ -182,14 +200,14 @@ public List<String> getMultiBulkReply() {
public List<byte[]> getBinaryMultiBulkReply() {
flush();
pipelinedCommands--;
return (List<byte[]>) protocol.read(inputStream);
return (List<byte[]>) read();
}

@SuppressWarnings("unchecked")
public List<Object> getObjectMultiBulkReply() {
flush();
pipelinedCommands--;
return (List<Object>) protocol.read(inputStream);
return (List<Object>) read();
}

public List<Object> getAll() {
Expand All @@ -200,7 +218,7 @@ public List<Object> getAll(int except) {
List<Object> all = new ArrayList<Object>();
flush();
while (pipelinedCommands > except) {
all.add(protocol.read(inputStream));
all.add(read());
pipelinedCommands--;
}
return all;
Expand All @@ -209,6 +227,6 @@ public List<Object> getAll(int except) {
public Object getOne() {
flush();
pipelinedCommands--;
return protocol.read(inputStream);
return read();
}
}
}

0 comments on commit 4bc292e

Please sign in to comment.