按照前面springMVC集成redis(1)、(2)(3)等3篇博文,在使用了几天后,会抛出“Could not get a resource from the pool”的异常,网上各种查资料,按照网上说的方法几乎都尝试了,但就是没有解决问题,这个问题被困扰了好久。最后得以解决,特总结如下:
上网查询原因,大多数报错的原因如下:
1、确保redis服务是否正常启动。
2.由于防火墙原因无法连接到Redis;确保redis所在的ip网络畅通,端口开放。
3.IP地址或端口错误
4.Jedis 对象用完以后,要释放掉,不让会一直占用,所以会出现无法获取新的资源。
5.Spring Boot项目,缺少依赖
如果使用Redis与Spring Boot,也会抛出此异常。
如果你使用的是Spring Boot,那么Redis的依赖是不够的,
您还需要从redis.io手动下载并安装Redis,并运行redis,您需要在使用Redis的所有应用程序中添加相关行:
application.properties:
spring.redis.host:
spring.redis.port: <端口>
6.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
7.当jedispool中的jedis被取完 等待超过你设置的 MaxWaitMillis 就会抛出Could not get a resource from t