论坛首页 综合技术论坛

Redis一个异常的解决办法,异常描述:Could not get a resource from the pool

浏览 32598 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-03-29   最后修改:2012-03-29
异常描述:
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
	at redis.clients.util.Pool.getResource(Pool.java:22)
	at com.derbysoft.jredis.longkeytest.BorrowObject.run(BorrowObject.java:22)
	at java.lang.Thread.run(Thread.java:662)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
	at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1134)
	at redis.clients.util.Pool.getResource(Pool.java:20)
	... 2 more


1、产生原因:客户端去redis服务器拿连接(代码描述的是租用对象borrowObject)的时候,池中无可用连接,即池中所有连接被占用,且在等待时候设定的超时时间后还没拿到时,报出此异常。

2、解决办法:调整JedisPoolConfig中maxActive为适合自己系统的阀值。

<bean id="dataJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive" value="300"/>
       <property name="maxIdle" value="100"/>
        <property name="maxWait" value="10000"/>
        <property name="testOnBorrow" value="true"/>
</bean>


3、重现:

public class BorrowObject implements Runnable {
    private ShardedJedisPool jedisPool;

    public BorrowObject(ShardedJedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Override
    public void run() {
        ShardedJedis shardedJedis = null;
        try {
            shardedJedis = jedisPool.getResource();
            String value = shardedJedis.hget("LONG_KEY_TEST:AA059E03E0AB7D806E6C351F87404B06C1190", "Roc El Pinar Aparthotel");
            System.out.println(value);
        } catch (Exception e) {
            //logger.error(e);
            e.printStackTrace();
        } finally {
            jedisPool.returnResource(shardedJedis);
        }
    }
}

public class BorrowObjectTest {
    private ShardedJedisPool jedisPool = null;

    public BorrowObjectTest() {
        List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
        JedisShardInfo jedisShardInfo = new JedisShardInfo("192.168.1.112");
        jedisShardInfo.setTimeout(1000000);
        jedisShardInfos.add(jedisShardInfo);
        jedisPool = new ShardedJedisPool(createJedisConfig(), jedisShardInfos);
    }

    private JedisPoolConfig createJedisConfig() {
        JedisPoolConfig jedisConfig = new JedisPoolConfig();
        jedisConfig.setMaxActive(2);
        jedisConfig.setMaxIdle(2);
        jedisConfig.setMaxWait(5);
        jedisConfig.setTestOnBorrow(true);
        return jedisConfig;
    }

    public static void main(String[] args) {
        BorrowObjectTest borrowObjectTest = new BorrowObjectTest();
        for (int i = 0; i < 300; i++) {
            new Thread(new BorrowObject(borrowObjectTest.jedisPool)).start();
        }
    }
}

   发表时间:2012-04-03  
问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接有多少,之类的
0 请登录后投票
   发表时间:2012-04-13   最后修改:2012-04-13
我也遇到这个问题,把shardedJedis设置成私有变量就解决了,不要总是调用jedisPool.getResource()
0 请登录后投票
   发表时间:2012-12-24  
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.commons.redis.cache.RedisManager.set(RedisManager.java:122)
at com.commons.redis.cache.RedisTest.main(RedisTest.java:6)
Caused by: java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1189)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 2 more

这个问题如何解决??
0 请登录后投票
   发表时间:2013-03-18  
leiyongping88 写道
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.commons.redis.cache.RedisManager.set(RedisManager.java:122)
at com.commons.redis.cache.RedisTest.main(RedisTest.java:6)
Caused by: java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1189)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 2 more

这个问题如何解决??


请问这个问题解决了吗?
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics