HTTP Status 500 - Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
type Exception report
message Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:97)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:143)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:41)
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
org.springframework.data.redis.core.DefaultBoundValueOperations.get(DefaultBoundValueOperations.java:41)
com.auth.provider.impl.DefaultRememberMeProvider.findRememberMeById(DefaultRememberMeProvider.java:47)
com.auth.AbstractSessionManager.getRememberMe(AbstractSessionManager.java:254)
com.auth.filter.AuthenticationFilter.doFilterInternal(AuthenticationFilter.java:84)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
root cause
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
redis.clients.util.Pool.getResource(Pool.java:42)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:90)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:143)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:41)
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
org.springframework.data.redis.core.DefaultBoundValueOperations.get(DefaultBoundValueOperations.java:41)
com.auth.provider.impl.DefaultRememberMeProvider.findRememberMeById(DefaultRememberMeProvider.java:47)
com.auth.AbstractSessionManager.getRememberMe(AbstractSessionManager.java:254)
com.auth.filter.AuthenticationFilter.doFilterInternal(AuthenticationFilter.java:84)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
root cause
redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
redis.clients.jedis.Connection.connect(Connection.java:142)
redis.clients.jedis.BinaryClient.connect(BinaryClient.java:75)
redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1724)
redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:65)
org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:836)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:434)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
redis.clients.util.Pool.getResource(Pool.java:40)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:90)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:143)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:41)
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
org.springframework.data.redis.core.DefaultBoundValueOperations.get(DefaultBoundValueOperations.java:41)
com.auth.provider.impl.DefaultRememberMeProvider.findRememberMeById(DefaultRememberMeProvider.java:47)
com.auth.AbstractSessionManager.getRememberMe(AbstractSessionManager.java:254)
com.auth.filter.AuthenticationFilter.doFilterInternal(AuthenticationFilter.java:84)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
root cause
java.net.ConnectException: Connection refused: connect
java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
java.net.AbstractPlainSocketImpl.connect(Unknown Source)
java.net.PlainSocketImpl.connect(Unknown Source)
java.net.SocksSocketImpl.connect(Unknown Source)
java.net.Socket.connect(Unknown Source)
redis.clients.jedis.Connection.connect(Connection.java:137)
redis.clients.jedis.BinaryClient.connect(BinaryClient.java:75)
redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1724)
redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:65)
org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:836)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:434)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
redis.clients.util.Pool.getResource(Pool.java:40)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:90)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:143)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:41)
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
org.springframework.data.redis.core.DefaultBoundValueOperations.get(DefaultBoundValueOperations.java:41)
com.auth.provider.impl.DefaultRememberMeProvider.findRememberMeById(DefaultRememberMeProvider.java:47)
com.auth.AbstractSessionManager.getRememberMe(AbstractSessionManager.java:254)
com.auth.filter.AuthenticationFilter.doFilterInternal(AuthenticationFilter.java:84)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.
Apache Tomcat/7.0.54
产生此错误的原因通常是:
一、Redis没有启动;
我自己遇到一次这样的问题。汗!
二、由于防火墙原因无法连接到Redis;
1、服务器防火墙入站规则。
2、访问Redis的应用程序所在主机的出站规则。
三、IP地址或端口错误
四、Jedis 对象用完以后,要释放掉,不让会一直占用,所以会出现无法获取新的资源。
五、Spring Boot项目,缺少依赖
如果使用Redis与Spring Boot,也会抛出此异常。
如果你使用的是Spring Boot,那么Redis的依赖是不够的,
您还需要从redis.io手动下载并安装Redis,然后将其从终端运行
me@my_pc:/path/to/redis/dir$ ./src/redis-server ./redis.conf
运行服务器后,您需要在使用Redis的所有应用程序中添加相关行:
spring.redis.host: // usually localhost, but can also be on a LAN
spring.redis.port: // usually 6379, but settable in redis.conf
application.yml:
...
spring:
redis:
host: // usually localhost, but can also be on a LAN
port: // usually 6379, but settable in redis.conf
六、vm.overcommit_memory = 0,fork失败
使用redis-cli,执行ping命令,异常信息出来了:
(error)MISCONF Redis is configured to save RDB snapshots, but is currently
not able to persist on disk. Commands that may modify the data set
are disabled. Please check Redis logs for details about the error.
然后查看redis日志,出现了
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
问题已经很清晰了,bgsave会fork一个子进程,因为vm.overcommit_memory = 0,所以申请的内存大小和父进程的一样,由于redis已经使用了60%的内存空间,所以fork失败
解决办法:
/etc/sysctl.conf 添加 vm.overcommit_memory=1
sysctl vm.overcommit_memory=1
作者:hhhh2
链接:http://www.jianshu.com/p/bb552ccc43b9
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
七、当jedispool中的jedis被取完 等待超过你设置的 MaxWaitMillis 就会抛出Could not get a resource from the pool
八、连接池中空闲的连接过一阵子就会自动断开,但是连接池还以为连接正常
参考配置文件:
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(200);
config.setMaxIdle(50);
config.setMinIdle(8);//设置最小空闲数
config.setMaxWaitMillis(10000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
//Idle时进行连接扫描
config.setTestWhileIdle(true);
//表示idle object evitor两次扫描之间要sleep的毫秒数
config.setTimeBetweenEvictionRunsMillis(30000);
//表示idle object evitor每次扫描的最多的对象数
config.setNumTestsPerEvictionRun(10);
//表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
config.setMinEvictableIdleTimeMillis(60000);
JedisPool pool = new JedisPool(config, ip, port, 10000, "密码", 0);