Fixed EOFException issue with JedisPool. Was trying to reuse a broken Jedis
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -787,4 +787,8 @@ public class Jedis {
|
||||
client.configSet(parameter, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return client.isConnected();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,7 +287,7 @@ public abstract class FixedResourcePool<T> {
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
} // If the wait gets interrupted, doesn't matter but print it (just
|
||||
// in case).
|
||||
// in case).
|
||||
} while (true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user