Fix Jedis conns not returning to pool on failed select
DATAREDIS-213
This commit is contained in:
@@ -205,26 +205,29 @@ 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)
|
||||
Exception exc = null;
|
||||
if (isQueueing()) {
|
||||
|
||||
@@ -20,12 +20,16 @@ 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.data.redis.connection.ReturnType;
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* Integration test of {@link JedisConnection}
|
||||
*
|
||||
@@ -187,6 +191,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)
|
||||
@@ -195,6 +200,22 @@ 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();
|
||||
}
|
||||
|
||||
@Test(expected=InvalidDataAccessApiUsageException.class)
|
||||
|
||||
@@ -24,12 +24,16 @@ import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.AbstractConnectionPipelineIntegrationTests;
|
||||
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* Integration test of {@link JedisConnection} pipeline functionality
|
||||
*
|
||||
@@ -118,6 +122,24 @@ public class JedisConnectionPipelineIntegrationTests extends
|
||||
assertEquals(Arrays.asList(new Object[] {"somethingelse"}), execResults);
|
||||
}
|
||||
|
||||
@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=UnsupportedOperationException.class)
|
||||
public void testPExpire() {
|
||||
|
||||
Reference in New Issue
Block a user