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 String host;
|
||||||
private int port = Protocol.DEFAULT_PORT;
|
private int port = Protocol.DEFAULT_PORT;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private boolean connected = false;
|
|
||||||
private Protocol protocol = new Protocol();
|
private Protocol protocol = new Protocol();
|
||||||
private DataOutputStream outputStream;
|
private DataOutputStream outputStream;
|
||||||
private DataInputStream inputStream;
|
private DataInputStream inputStream;
|
||||||
@@ -85,10 +84,9 @@ public class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void connect() throws UnknownHostException, IOException {
|
public void connect() throws UnknownHostException, IOException {
|
||||||
if (!connected) {
|
if (!isConnected()) {
|
||||||
socket = new Socket(host, port);
|
socket = new Socket(host, port);
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
connected = socket.isConnected();
|
|
||||||
outputStream = new DataOutputStream(socket.getOutputStream());
|
outputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
inputStream = new DataInputStream(new BufferedInputStream(socket
|
inputStream = new DataInputStream(new BufferedInputStream(socket
|
||||||
.getInputStream()));
|
.getInputStream()));
|
||||||
@@ -96,7 +94,7 @@ public class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
if (connected) {
|
if (isConnected()) {
|
||||||
try {
|
try {
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
@@ -106,12 +104,13 @@ public class Connection {
|
|||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new JedisException(ex);
|
throw new JedisException(ex);
|
||||||
}
|
}
|
||||||
connected = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return connected;
|
return socket != null && socket.isBound() && !socket.isClosed()
|
||||||
|
&& socket.isConnected() && !socket.isInputShutdown()
|
||||||
|
&& !socket.isOutputShutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getStatusCodeReply() {
|
protected String getStatusCodeReply() {
|
||||||
|
|||||||
@@ -787,4 +787,8 @@ public class Jedis {
|
|||||||
client.configSet(parameter, value);
|
client.configSet(parameter, value);
|
||||||
return client.getStatusCodeReply();
|
return client.getStatusCodeReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return client.isConnected();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -41,16 +41,22 @@ public class JedisPool extends FixedResourcePool<Jedis> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void destroyResource(Jedis jedis) {
|
protected void destroyResource(Jedis jedis) {
|
||||||
jedis.quit();
|
if (jedis.isConnected()) {
|
||||||
try {
|
try {
|
||||||
|
jedis.quit();
|
||||||
jedis.disconnect();
|
jedis.disconnect();
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
throw new JedisException(e);
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isResourceValid(Jedis jedis) {
|
protected boolean isResourceValid(Jedis jedis) {
|
||||||
return jedis.ping().equals("PONG");
|
try {
|
||||||
|
return jedis.isConnected() && jedis.ping().equals("PONG");
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package redis.clients.jedis.tests;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@@ -36,4 +37,51 @@ public class JedisPoolTest extends Assert {
|
|||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
|
||||||
|
JedisPool pool = new JedisPool("localhost");
|
||||||
|
pool.setResourcesNumber(1);
|
||||||
|
pool.init();
|
||||||
|
|
||||||
|
Jedis jedis = pool.getResource(200);
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.set("foo", "0");
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
|
||||||
|
jedis = pool.getResource(200);
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.incr("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
|
||||||
|
IOException {
|
||||||
|
JedisPool pool = new JedisPool("localhost");
|
||||||
|
pool.setResourcesNumber(1);
|
||||||
|
pool.init();
|
||||||
|
|
||||||
|
Jedis jedis = pool.getResource(200);
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.quit();
|
||||||
|
pool.returnBrokenResource(jedis);
|
||||||
|
|
||||||
|
jedis = pool.getResource(200);
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.incr("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = TimeoutException.class)
|
||||||
|
public void checkPoolOverflow() throws TimeoutException {
|
||||||
|
JedisPool pool = new JedisPool("localhost");
|
||||||
|
pool.setResourcesNumber(1);
|
||||||
|
pool.init();
|
||||||
|
|
||||||
|
Jedis jedis = pool.getResource(200);
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.set("foo", "0");
|
||||||
|
|
||||||
|
jedis = pool.getResource(200);
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.incr("foo");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user