最近跟着尚硅谷学Redis,在他最新的Redis教程中提供的资料代码和所有的操作都是基于没有给Redis设置密码的,而我设置了密码,所以经常跟着视频敲代码时忘了这事
运行时会出现如下错误
Exception in thread "main" redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:51)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at com.atguigu.SecKill_redis.main(SecKill_redis.java:27)
Caused by: java.util.NoSuchElementException: Unable to validate object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:506)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 2 more
此时如果Redis设置了密码,则要检查程序连接Redis的地方是否输入了密码
如:
1、直接连接时
Jedis jedis =new Jedis("网络地址",6379);
jedis.auth("password");
2、使用JedisPoolUtil 工具类连接时,在new JedisPool类时输入密码
jedisPool = new JedisPool(masterName, sentinels, createJedisConfig(), redisConnTimeout,"password");
public static JedisPool getJedisPoolInstance() {
if (null == jedisPool) {
synchronized (JedisPoolUtil.class) {
if (null == jedisPool) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(200);
poolConfig.setMaxIdle(32);
poolConfig.setMaxWaitMillis(100*1000);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setTestOnBorrow(true); // ping PONG
//在此处设置密码
jedisPool = new JedisPool(masterName, sentinels, createJedisConfig(), redisConnTimeout ,"password");
}
}
}
return jedisPool;
}
|