redis提示Could not get a resource from the pool
jredis连接池不能找到,但是最初启动的时候是开始的,运行一段时间之后就会报下面错误,求解?
报错:
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.jiuzhekan.util.RedisConfig.getJedis(RedisConfig.java:49)
at com.jiuzhekan.util.MyRedisUtilTest.testString(MyRedisUtilTest.java:21)
at com.jiuzhekan.util.MyRedisUtilTest.main(MyRedisUtilTest.java:98)
Caused by: java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1203)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 3 more
Exception in thread "main" java.lang.NullPointerException
at com.jiuzhekan.util.MyRedisUtilTest.testString(MyRedisUtilTest.java:22)
at com.jiuzhekan.util.MyRedisUtilTest.main(MyRedisUtilTest.java:98)
连接池构建如下
public final class RedisConfig {
//Redis服务器IP
private static String ADDR = "121.199.**.**";
//Redis的端口号
private static int PORT = 6379;
//访问密码
private static String AUTH = "jiuzhekan_redis_password";
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnResourceObject(jedis);
}
}
}
每次调用的时候最终都会释放资源:
public static <T> List<T> getList(String key){
Jedis jedis=getJedis();
List<T> list=null;
try{
if(jedis== null || !jedis.exists(key.getBytes())){
return null;
}
byte[] in =jedis.get(key.getBytes());
list= (List<T>) ObjectTranscoder.deserialize(in);
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
RedisConfig.returnResource(jedis);
}
return list;
}
求解?
报错:

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.jiuzhekan.util.RedisConfig.getJedis(RedisConfig.java:49)
at com.jiuzhekan.util.MyRedisUtilTest.testString(MyRedisUtilTest.java:21)
at com.jiuzhekan.util.MyRedisUtilTest.main(MyRedisUtilTest.java:98)
Caused by: java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1203)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 3 more
Exception in thread "main" java.lang.NullPointerException
at com.jiuzhekan.util.MyRedisUtilTest.testString(MyRedisUtilTest.java:22)
at com.jiuzhekan.util.MyRedisUtilTest.main(MyRedisUtilTest.java:98)
连接池构建如下
public final class RedisConfig {
//Redis服务器IP
private static String ADDR = "121.199.**.**";
//Redis的端口号
private static int PORT = 6379;
//访问密码
private static String AUTH = "jiuzhekan_redis_password";
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnResourceObject(jedis);
}
}
}
每次调用的时候最终都会释放资源:
public static <T> List<T> getList(String key){
Jedis jedis=getJedis();
List<T> list=null;
try{
if(jedis== null || !jedis.exists(key.getBytes())){
return null;
}
byte[] in =jedis.get(key.getBytes());
list= (List<T>) ObjectTranscoder.deserialize(in);
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
RedisConfig.returnResource(jedis);
}
return list;
}
求解?
...全文
相关推荐
分布式实现redis锁 出现错误Could not get a resource from the pool
本文详细介绍了Spring Boot项目的Maven配置文件(POM),包括项目基本信息的设置、依赖引入以及构建插件的配置,旨在帮助开发者更好地理解和使用Maven管理Spring Boot项目。
jedis-2.9.0.jar和commons-pool2-2.6.0.jar下载(jedis连接redis数据库)
```java Jedis resource = jedisPool.getResource(); String value = resource.get("key"); resource.close(
Redis 的高并发实战:抢购系统 1
当引擎层出现慢查询时,响应时间会延长,可能导致连接池耗尽,引发“Could not get a resource from the pool”的异常。
Redis的开发规范和常见问题1
如果因慢查询导致连接返回延迟,连接池可能会耗尽,引发“Could not get a resource from the pool”的错误。
详解java 客户端链接不上redis解决方案
**Redis服务状态**:确认Redis服务器在Linux虚拟机中已启动。可以通过`systemctl status redis`或`service redis status`命令来检查。2.


Java SE

