使用redis也有端时间了,现在讲开发中遇到的几个常见异常总结如下:
一、通过JedisPool类实例获取getResource()时抛出can't get a resource异常。
异常代码如下:
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
分析:
redis.clients.util.Pool.getResource会从JedisPool实例池中返回一个可用的redis连接。分析源码可知JedisPool extends redis.clients.util.Pool .而Pool是通过
commons-pool开源工具包中的org.apache.commons.pool2.impl.GenericObjectPool来实现对Jedis实例的管理的。所以我们分析一下GenericObjectPool或许能找到答案。
其中三个重要个几个属性是:
MaxActive: 最大连接数。
MaxIdle: 最大空闲数。
MaxWait: 最大等待时间,单位毫秒(million seconds)。
当连接池中无可用连接时会会进行等待maxWait时间,若超出泽抛Could not get a resource from the pool异常。
所以应根据程序实际情况合理设置这三个参数的值,尽量避免这个异常。

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



