Redis an unusual workaround, exception description: Could not get a resource from the pool

Source: Internet
Author: User
Tags redis redis server
Exception Description:
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, the reason: the client to the Redis server to get the connection (the code describes the lease object Borrowobject), there is no connection in the pool, that is, all connections in the pool is occupied, and in the waiting time after the timeout time set to not get, reported this exception.

2, the solution: adjust the jedispoolconfig in the maxactive to suit their own system threshold.

<bean id= "Datajedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >
<property name= "maxactive" value= "/>"
<property name= "Maxidle" value= "/>"
<property name= "maxwait" value= "10000"/>
<property name= "Testonborrow" value= "true"/>
</bean>

3. Reproduce:

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 <; i++) {New Thread (new Borrowobject (Borrowobjecttest.jedispool)). Start ();
   } }
} 
 reprinted from: http://www.iteye.com/topic/1122212 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.