问题
问题1:could not get a resource from the pool
问题2:JedisConnectionException:java.net.SocketTimeoutException:Read timed out
背景
测试在测试的过程中,用grep error 操作,我之前还没有遇到过这样的测试,因为日志中存在error,而提单的。当然还是我们研发自己不够自信,如果自信相信随便测试怎么 grep。因为站的角度不一样,有时候也很无赖,想让同事们把代码写好,异常处理好,但是因为我没有xx的能力。所以也就目前这个形式。回到正题,日志中出现error那说明代码肯定是有问题的,这次就遇到了如题目中的问题。
复现
public class RedisFail implements Runable{
@Autiowired
private JedisPool jedisPool;
public RedisFail(JedisPool jedisPool){
this.jedisPool = jedisPool;
}
@Override
public void run(){
Jedis jedis = null ;
try{
jedis = jedisPool.getResource();
String value = jedis.get("testRedisTimeOut");
}catch (Exception e){
e.printStackTrace();
} finally{
jedisPool.close();
}
}
}
public class RedisFailTest {
private JedisPool jedisPool;
public RedisFailTest (){
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(5);
poolConfig.setMaxTotal(10);
poolConfig.setMaxWaitMills(-1);
poolConfig.setTestOnBorrow(true);
// 最后一个参数 设置成 null
jedis = new JedisPool(poolConfig,host,port,timeout,password,database,clientName);
}
public static void main(String[] args){
RedisFailTest redisFailTest = new RedisFailTest ();
for(int i= 0;i<300;i++){
new Thead(new RedisFail(redisFailTest.jedisPool)).start();
}
}
}
只要是修改 线程的数量 超时时间 maxToatl的数据
验证
验证1:
验证2:
这个复现改下timeout参数值就可以。图中设置的5ms 改成5000 问题就可以解决了。环境中目前配置的是5000,这里只是为了复现问题改下。