JedisConnectionException: java.net.SocketException: Socket closed;Unknown reply: ; It seems like server has closed the connection.解决办法
最近在使用Jedis pool的时候,写好的servlet程序一经高并发的测试,就会抛出各种Exception,像JedisConnectionException: java.net.SocketException: Socket closed;Unknown reply: ; It seems like server has closed the connection.等等。在网上查了好多资料,很多都说和Redis的timeout的默认设置有关,timeout默认设置是300s,一个redis socket连接超过这个时限,但没有对redis做任何操作的话,redis server就会主动关闭了这个连接。一种解决办法是将timeout设置为0(无限制)就行了。
由于一些因素,redis一直无法设置然后进行重启。然后我就在timeout=300s的情况下,做了各种尝试,保证既可以使用jedis pool又不会在高并发的时候出错。怪我查找资料不力,一直没有找到解决的办法,只能放弃使用连接池,每次函数调用就new一个jedis对象,然后对redis进行操作。这种方法要注意:jedis.disconnect().这种方式下,虽然在高并发的时候会对redis server造成压力,但很稳定速度也挺快的。
后来再次试图解决之前的问题的时候,发现Jedis pool 还有两个参数: TestOnBorrow,在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的;TestOnReturn,在return一个jedis实例时,是否提前进行validate操作.
设置了这两个参数以后,问题就得到解决了,做并发测试没有什么问题。
[cpp] view plain copy
- //jedis pool 设置
- public static JedisPool pool;
- static{
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(con.getIntValue("redis.maxActive", 300));
- config.setMaxIdle(con.getIntValue("redis.maxIdle", 10));
- //config.setMinIdle(10);
- config.setMaxWait(con.getLongValue("redis.maxWait", 1000L));
- config.setTestOnBorrow(true);
- config.setTestOnReturn(true);
- //config.testWhileIdle=true;
- //config.minEvictableIdleTimeMillis=30000;
- //config.timeBetweenEvictionRunsMillis=30000;
- pool = new JedisPool(config, redisaddr, redisport);
- }
- //获取jedis pool 对象
- try{
- jee= pool.getResource();
- }catch(JedisConnectionException e){
- pool.returnBrokenResource(jee);
- }
- //读取redis
- if(jee!=null){
- try{
- stationList=jee.lrange("trps:lineinfo", 0, -1);
- } catch (Exception e) {
- pool.returnBrokenResource(jee);
- } finally{
- if(null != jee){
- pool.returnResource(jee);
- }
- }
- }
from:http://blog.csdn.net/wangzaizhen2009/article/details/50592826?locationNum=13&fps=1
文:Redis : It seems like server has closed the connection
Small problem : redis.clients.jedis.exceptions.JedisConnectionException: It seems like server has closed the connection.
At first ,I Don’t have to configure redis-config # maxclients
So , Get me wrong this problem
But , redis — info
# Clients
connected_clients:2
this is a bad idea , really, when redis server send back “Protocol error” message , the server will close socket; but jedis Protocol.java just throw a JedisDataException exception and ignored tcp close segment; so if you reuse this connection, jedis will throw an exception with “It seems like server has closed the connection” message.
maxidletime default value is five minutes,the server will continue to cycle detection clinet last communication time, if more than ,close and return source.then,the client sends the data to the server and problem occurs.
final solution : config.setTestOnBorrow(true);
中文:http://www.chepoo.com/jedis-connection-exception-it-seems-like-server-has-closed-the-connection.html
看提示,应该是服务端主动关闭了连接。查看线上redis服务器的配置的timout选项:
# Close the connection after a client is idle for N seconds (0 to disable) timeout 30 |
该配置指的是客户端连接空闲超过多少秒后,服务端主动关闭连接,默认值0表示服务端永远不主动关闭。客户端使用了一个连接池管理访问redis的所有连接,这些连接是长连接,当业务量较小时,客户端部分连接使用率较低,当两次使用之间的间隔超过30秒时,redis服务端就主动关闭了这个连接,而等客户端下次再使用这个连接对象时,发现服务端已经关闭了连接,进而报错。
连接池setTimeBetweenEvictionRunsMillis(60000);// 失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程 每隔1分钟检查一下连接池建立的连接。
解决方案:把timeout值大于60s即可。改为300,问题解决。
+
+
+
=
=
=
相关推荐
java.net.SocketException Connection reset 解决方法
JAVA.NET.SOCKETEXCEPTION TOO MANY OPEN FILES解决方法,三种方案,临时,永久,根治
java.net.SocketException: Unrecognized Windows Sockets error: 0: no Inet4Address associated with interface 本文提供了在Win7或Vista系统下解决这个异常信息的办法。
NULL 博文链接:https://zhoudan241.iteye.com/blog/1432014
最近纠结致死的一个java报错java.net.SocketException: Connection reset 终于得到解决
接口调用报错:java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class com.ibm.websphere.ssl.protocol.SSLSocketFactory
socket 文件同步
import java.net.Socket; public class TCPServer { public static void main(String[] args) { try { ServerSocket socket = new ServerSocket(4444); while (true) { Socket sc =socket.accept()...
System.Net.WebException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集 ---> System.Net.Sockets.SocketException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集 ...
34. import java.net.SocketException; 35. import java.net.UnknownHostException; 36. import java.util.Date; 37. import java.util.Scanner; 38. import java.util.Stack; 39. import java.util.prefs....
转:java.net.SocketException: Too many open files解决方法最近随着网站访问量的提高把web服务器移到linux下了,在移服务器的第二天,tomcat频繁的报 java.net.SocketException: Too many open files错误,错误...
1.解决服务器报错:java.net.SocketException: No buffer space available (maximum connections reached?): JVM_Bind 2.解决Windows Server 2003/2008 R2系统BUG导致windows缓冲区已满(no buffer space),Windows...
Google I/O 2013推荐Android 网络通信框架Volley 下载下来后然后编译为jar包,再在自己的工程里import进来。
注意:由于压缩包大于15M,所以分两部分上传,这是第一部分!下载SSH2-1后,再解压这两个文件后就可用了。
syslog4j Syslog4j提供了BSD Syslog协议(RFC 3164)和“结构化syslog”协议草案(RFC Draft)的客户端和服务器实现。 警告 这是使用的重新打包的fork,因为原始软件包没有发布到Maven Central的最新版本。...
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站...
System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 想到阿里云喜欢用安全组屏蔽端口,于是我就赶紧去配置了安全规则。进站出站都配置了,结果还是...
Linux下运行tomcat或者was出现java.net.SocketException: 打开的文件过多错误 tomcat报“too many open files”的错误,解决办法如下:
at Ice.ConnectionI$SocketReadyCallback.socketReady(ConnectionI.java:2299) at IceInternal.SelectorThread.run(SelectorThread.java:203) at IceInternal.SelectorThread$HelperThread.run(SelectorThread....
导入java.net.SocketException; 导入java.net.UnknownHostException; 导入java.nio.ByteBuffer; 导入java.util.Random; 公共类Servidor实现Runnable { Socket csocket; DatagramSocket cservSocket; ...