超时

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
at redis.clients.jedis.Protocol.process(Protocol.java:79)
at redis.clients.jedis.Protocol.read(Protocol.java:131)
at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:199)
at redis.clients.jedis.Jedis.hgetAll(Jedis.java:851)

If what you want to do is set Jedis connection timeout, you should do it using the special constructor made for that:

    public Jedis(final String host, final int port, final int timeout)

What you are doing is setting the timeout on redis settings from jedis.
Doing CONFIG SET timeout 60, means that redis will close idle client connections after 60 seconds.
That's why you get the exception in Jedis.

The timeout parameter of the constructor is in milliseconds because this value is affected internally to java.net.Socket#connect(java.net.SocketAddress, int) and java.net.Socket#setSoTimeout(int) methods. So this this value is connection and a socket read timeout.

This is a bit of an extension to xetorthio's answer, but here is similar approach for use with a JedisPool. (Caveat: this is based on my understanding from looking at the Jedis version 2.6.2 code directly and has not been tested in a live use case.)

    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxWaitMillis(writeTimeout);
JedisPool pool = JedisPool(jedisPoolConfig, redisHost, port, readTimeout);

The writeTimeout is max time for a Jedis resource from the pool to wait for a write operation.

The readTimeout specified for the pool constructor is the wait time for a socket read, see java.net.Socket.setSoTimeout for more specific details.

Few things to consider:

  1. For both Jedis and JedisPool classes, timeout is in miliseconds. Default timeout, at least in 2.5.1, as I see, is 2000 (milisec): int redis.clients.jedis.Protocol.DEFAULT_TIMEOUT = 2000 [0x7d0]

  2. As per this documentation, recent version of Redis does not close connection, even if the client is idle. I have not verified this yet, and I will try to update the post when I do.

http://stackoverflow.com/questions/14993644/configure-jedis-timeout

https://github.com/xetorthio/jedis/issues/1190

  1. redis server, no changes during all test
    redis_version:2.6.9

  2. We've been upgrading jedis client from 2.0.0 to 2.7.3, with the same configuration parameters, 
    we saw a lot more exceptions like below

Exception in thread "Thread-4" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:50)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:99)
at com.yaojuncn.jedis.JedisPerfTest$.onecall(JedisPerfTest.scala:94)
at com.yaojuncn.jedis.JedisPerfTest$$anonfun$runthreadtest$1$$anon$1$$anonfun$run$1.apply$mcVI$sp(JedisPerfTest.scala:69)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:141)
at com.yaojuncn.jedis.JedisPerfTest$$anonfun$runthreadtest$1$$anon$1.run(JedisPerfTest.scala:68)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:449)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
at redis.clients.util.Pool.getResource(Pool.java:48)
... 6 more
  1. after some hard time, finally reached the test code of below with 2.7.3 version,https://github.com/yaojuncn/jedis273concurrentperftest/blob/master/src/main/scala/com/yaojuncn/jedis/JedisPerfTest.scala

we saw a lot of pool.getResource invocations longer than 20ms.... (if we uncomment out line 37 of above source file we will see a lot of exceptions)
https://github.com/yaojuncn/jedis273concurrentperftest/blob/master/jedis2.7.3.sampleoutput.txt

with 2.0.0 version
https://github.com/yaojuncn/jedis200concurrentperftest/blob/master/src/main/scala/com/yaojuncn/jedis/JedisPerfTest.scala
the output is pretty clean
https://github.com/yaojuncn/jedis200concurrentperftest/blob/master/jedis2.0.0.sampleoutput.txt

  1. I guess this is related to the apache-common pool version that jedis is using and I do saw the FAQhttps://github.com/xetorthio/jedis/wiki/FAQ and issues listed here like
    #844

but by comparing with 2.7.3 and 2.0.0, I think there's something wrong now.
with 16 threads on a 8 max pool size, 
the 2.0.0 could perform very well while the 2.7.3 will see a lot of exceptions if we set the maxwait=20ms;

https://github.com/xetorthio/jedis/issues/1139

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool的更多相关文章

  1. 【java异常】redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

    产生此错误的原因通常是: 一.Redis没有启动: 我自己遇到一次这样的问题.汗! 二.由于防火墙原因无法连接到Redis; 1.服务器防火墙入站规则. 2.访问Redis的应用程序所在主机的出站规则 ...

  2. redis使用问题一:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause

    本文使用的是spring-data-redis 首先说下redis最简单得使用,除去配置. 需要在你要使用得缓存得地方,例如mybatis在mapper.xml中加入: <cache evict ...

  3. redis报错:java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)

    最近写了一个服务通过springboot构建,里面使用了redis作为缓存,发布到服务器运行成功,但是有时候会报redis的错误:org.springframework.data.redis.Redi ...

  4. redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: 断开的管道 (Write failed)

    昨晚,包发到测试环境中,出现redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: 断开的 ...

  5. Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out

    问题: java连接不上redis. 异常信息: Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.ne ...

  6. Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)

    一.linux中配置redis,使用java连接测试时报错: Exception in thread "main" redis.clients.jedis.exceptions.J ...

  7. redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect time out

    redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect ti ...

  8. 【java异常】redis.clients.jedis.exceptions.JedisConnectionException: Could not get a res

    产生此错误的原因通常是: 一.Redis没有启动: 我自己遇到一次这样的问题.汗! 二.由于防火墙原因无法连接到Redis; 1.服务器防火墙入站规则. 2.访问Redis的应用程序所在主机的出站规则 ...

  9. 【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection lost

    问题情形 当在执行Redis一直指令时,有可能会遇见如下几种错误: 1) redis.clients.jedis.exceptions.JedisConnectionException: Could ...

随机推荐

  1. The Most Wanted Letter

    The Most Wanted Letter You are given a text, which contains different english letters and punctuatio ...

  2. 107个常用Javascript语句

    1.document.write( " "); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document- >html- >(head,body ...

  3. Static用法

    一.Static全局变量和全局变量的区别 1)全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式. 这两者在存储 ...

  4. C++: int和string相互转换

    假设在一个C++的程序中常常会用到int和string之间的互换.个人建议能够写成一个函数,下次用的时候直接调用就可以. #include <iostream> #include < ...

  5. Apple Swfit UI控件实现

    不下载你会懊悔的~~ 下载地址:https://github.com/HunkSmile/Swift.git // UILabel var label = UILabel(frame: self.vi ...

  6. [HeadFrist-HTMLCSS学习笔记][第一章Web语言:开始了解HTML]

    head title body 元素= 开始标记 + 内容 +结束标记 还能给段落一个变量名 <p id="houseblend"> body </p> s ...

  7. CSS兼容性常见问题总结

    DIV+CSS设计IE6.IE7.FF 兼容性 DIV+CSS网页布局这是一种趋势,我也开始顺应这股趋势了,不过在使用DIV+CSS网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使 ...

  8. html进阶css(4)

    盒子模型-边框 首先请看下图 <!doctype html> <html> <head> <meta charset="utf-8"> ...

  9. Android 定义重名权限问题

    一直以来对android的权限机制就有一个疑问,因为在使用权限时,实际上只需要permission的name这一个标签,而在定义权限时,android是不会检查是否重名的,那么在两个应用定义了重名权限 ...

  10. Java实现的 线程池

    由于最近开始学习java,用到了线程池,按照之前c++的写法写出此java版的线程池 TaskRunnale实现相关任务的接口,具体要实现什么任务在相应的run函数中实现. package threa ...