在使用JedisPool时配置JedisPoolConfig的testOnBorrow为true时,就会报出
以下是配置代码:
JedisPoolConfig config = new JedisPoolConfig();
// 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxActive(500);
config.whenExhaustedAction=2;
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(5);
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWait(1000 * 100);
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
this.pool = new JedisPool(config, host, Integer.parseInt(port));
解决方法:
之前这么配置使用都没问题,后来我给redis设置了连接密码,这样配置后就死活不行了,于是切换了一台没有设密码的redis这样连接时没问题的,
于是想到在初始化JedisPool时没有设置密码,我就看了一下JedisPool的构造函数有密码这个参数,如下:
new JedisPool(config, host, Integer.parseInt(port),1000 * 100,“你的密码”);
这样设置后再次使用就没有问题了!