Fix Jedis conns not returning to pool on failed select

DATAREDIS-213
This commit is contained in:
Jennifer Hickey
2013-07-26 14:47:49 -04:00
parent 352620d054
commit a063904c54
4 changed files with 62 additions and 13 deletions

View File

@@ -138,7 +138,6 @@ public class JedisConnection implements RedisConnection {
broken = true;
return JedisUtils.convertJedisAccessException((IOException) ex);
}
return new RedisSystemException("Unknown jedis exception", ex);
}
@@ -173,24 +172,28 @@ public class JedisConnection implements RedisConnection {
public void close() throws DataAccessException {
// return the connection to the pool
try {
if (pool != null) {
if (!broken) {
// reset the connection
if (pool != null) {
if (!broken) {
// reset the connection
try {
if (dbIndex > 0) {
select(0);
jedis.select(0);
}
pool.returnResource(jedis);
return;
}catch(Exception ex) {
DataAccessException dae = convertJedisAccessException(ex);
if(broken) {
pool.returnBrokenResource(jedis);
}else {
pool.returnResource(jedis);
}
throw dae;
}
}else {
pool.returnBrokenResource(jedis);
return;
}
} catch (Exception ex) {
// exceptions are handled below
}
if (pool != null && broken) {
pool.returnBrokenResource(jedis);
return;
}
// else close the connection normally (doing the try/catch dance)

View File

@@ -22,10 +22,14 @@ import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.JedisPoolConfig;
/**
* Integration test of {@link JedisConnection}
*
@@ -84,6 +88,7 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
factory2.afterPropertiesSet();
// No way to really verify we are in the selected DB
factory2.getConnection().ping();
factory2.destroy();
}
@Test(expected=InvalidDataAccessApiUsageException.class)
@@ -92,5 +97,21 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
factory2.setDatabase(77);
factory2.afterPropertiesSet();
factory2.getConnection();
factory2.destroy();
}
@Test
public void testClosePool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(1);
config.setMaxWait(1l);
JedisConnectionFactory factory2 = new JedisConnectionFactory(config);
factory2.setHostName(SettingsUtils.getHost());
factory2.setPort(SettingsUtils.getPort());
factory2.afterPropertiesSet();
RedisConnection conn2 = factory2.getConnection();
conn2.close();
factory2.getConnection();
factory2.destroy();
}
}

View File

@@ -28,12 +28,15 @@ import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.AbstractConnectionPipelineIntegrationTests;
import org.springframework.data.redis.connection.DefaultStringTuple;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection.StringTuple;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Tuple;
/**
@@ -99,6 +102,24 @@ public class JedisConnectionPipelineIntegrationTests extends
public void testMultiDiscard() {
}
@Test
//DATAREDIS-213 - Verify connection returns to pool after select
public void testClosePoolPipelinedDbSelect() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(1);
config.setMaxWait(1l);
JedisConnectionFactory factory2 = new JedisConnectionFactory(config);
factory2.setHostName(SettingsUtils.getHost());
factory2.setPort(SettingsUtils.getPort());
factory2.setDatabase(1);
factory2.afterPropertiesSet();
RedisConnection conn2 = factory2.getConnection();
conn2.openPipeline();
conn2.close();
factory2.getConnection();
factory2.destroy();
}
// Unsupported Ops
@Test(expected = RedisSystemException.class)
public void testBitSet() throws Exception {

View File

@@ -90,6 +90,10 @@ public class JedisConnectionTransactionIntegrationTests extends
public void testOpenPipelineTwice() {
}
@Ignore
public void testClosePoolPipelinedDbSelect() {
}
// Unsupported Ops
@Test(expected = RedisSystemException.class)