Fixed EOFException issue with JedisPool. Was trying to reuse a broken Jedis

This commit is contained in:
Jonathan Leibiusky
2010-09-04 11:29:54 -03:00
parent 00dedc5f25
commit 68905396d5
5 changed files with 70 additions and 13 deletions

View File

@@ -14,7 +14,6 @@ public class Connection {
private String host;
private int port = Protocol.DEFAULT_PORT;
private Socket socket;
private boolean connected = false;
private Protocol protocol = new Protocol();
private DataOutputStream outputStream;
private DataInputStream inputStream;
@@ -85,10 +84,9 @@ public class Connection {
}
public void connect() throws UnknownHostException, IOException {
if (!connected) {
if (!isConnected()) {
socket = new Socket(host, port);
socket.setSoTimeout(timeout);
connected = socket.isConnected();
outputStream = new DataOutputStream(socket.getOutputStream());
inputStream = new DataInputStream(new BufferedInputStream(socket
.getInputStream()));
@@ -96,7 +94,7 @@ public class Connection {
}
public void disconnect() {
if (connected) {
if (isConnected()) {
try {
inputStream.close();
outputStream.close();
@@ -106,12 +104,13 @@ public class Connection {
} catch (IOException ex) {
throw new JedisException(ex);
}
connected = false;
}
}
public boolean isConnected() {
return connected;
return socket != null && socket.isBound() && !socket.isClosed()
&& socket.isConnected() && !socket.isInputShutdown()
&& !socket.isOutputShutdown();
}
protected String getStatusCodeReply() {

View File

@@ -787,4 +787,8 @@ public class Jedis {
client.configSet(parameter, value);
return client.getStatusCodeReply();
}
public boolean isConnected() {
return client.isConnected();
}
}

View File

@@ -41,16 +41,22 @@ public class JedisPool extends FixedResourcePool<Jedis> {
@Override
protected void destroyResource(Jedis jedis) {
jedis.quit();
try {
jedis.disconnect();
} catch (IOException e) {
throw new JedisException(e);
if (jedis.isConnected()) {
try {
jedis.quit();
jedis.disconnect();
} catch (Exception e) {
}
}
}
@Override
protected boolean isResourceValid(Jedis jedis) {
return jedis.ping().equals("PONG");
try {
return jedis.isConnected() && jedis.ping().equals("PONG");
} catch (Exception ex) {
return false;
}
}
}