JedisException:Could not get a resource from the pool

最近跟着尚硅谷学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;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值