Could not get a resource in Redis from the pool exception resolution

Source: Internet
Author: User
Tags getmessage redis redis server

Using Redis as a cache in your project, the following error occurs after running for a period of time: Could not get a resource from the pool, and then look at the specific exception information is jedispool to get the Jedis object, This means that there are no Jedis available in the connection pool.

Their first response is to increase the maximum number of links (setmaxtotal), just started to set 100, later 200, in later 2000 are not

And then the Internet a search found that everyone's answer is also to modify the maximum number of connections, the following demo is an online blog explanation:

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" >
        [color=red]<property Name= "maxactive" value= "/>[/color" <property name=
        "Maxidle" value= "the"/> <property
        Name = "Maxwait" value= "10000"/>
        <property name= "Testonborrow" value= "true"/>
</bean>
But this self has also been set, configured as follows:

#最大活动对象数     
redis.pool.maxtotal=1000    
#最大能够保持idel状态的对象数      
redis.pool.maxidle=100  
#最小能够保持idel状态的对象数   
redis.pool.minidle=50    
#当池内没有返回对象时, maximum wait time    
redis.pool.maxwaitmillis=10000    
#当调用borrow The object method, Whether the validity check is performed    
when Redis.pool.testonborrow=true #当调用return the object method    
Redis.pool.testonreturn=true  
# "Idle link" detects the thread, the period of detection, and the number of milliseconds. A negative value indicates that the detection thread is not running. The default is -1.  
redis.pool.timebetweenevictionrunsmillis=30000  
whether to detect idle timeouts when #向调用者输出 a "linked" object;  
redis.pool.testwhileidle= True  
# The number of linked resources detected each time for the "Idle link" detection thread. The default is 3.  
Redis.pool.numtestsperevictionrun=50  
#表示一个对象至少停留在idle状态的最短时间 before it can be scanned and evicted by the idle object Evitor This is only meaningful if the Timebetweenevictionrunsmillis is greater than 0
minevictableidletimemillis=60000
#redis服务器的IP    
Redis.ip=xxxxxx  
#redis服务器的Port    
redis1.port=6379   
But after this setting, when running for a period of time will still report the same error, say the words do not love the maximum number of links "1000" This value is really not small, but still wrong, so certainly not the reason for this value.

Later on the internet found an article, the article in the Jedis tool class has three methods, the code is as follows:

public class Jedisutils {
	private static Log logger = Logfactory.getlog (jedisutils.class);

	/**
	 * Automatically injects Redis connection instance object thread pool
	 */
	@Autowired
	private Jedispool jedispool;

	/**
	 * Get Jedis object
	 * 
	 * @return
	 *
	/public synchronized Jedis Getjedis () {
		Jedis Jedis = null;< C13/>if (Jedispool! = null) {
			try {
				if (Jedis = = null) {
					Jedis = Jedispool.getresource ();}
			} cat CH (Exception e) {
				logger.error (E.getmessage (), E);
			}
		}
		return Jedis;
	}

	/**
	 * Reclaim Jedis Object Resource
	 * 
	 * @param jedis
	 *
	/public synchronized void Returnresource (Jedis Jedis) { C30/>if (Jedis! = null) {
			jedispool.returnresource (Jedis);
		}
	}

	/**
	 * Jedis object is an exception, Reclaim Jedis object Resource
	 * 
	 * @param Jedis */public
	synchronized void Returnbrokenresource (Jedis Jedis) {
		if (Jedis! = null) {
			jedispool.returnbrokenresource (Jedis);
		}

	}
}

There are two recovery methods found, one is the normal end of the call to release the Jedis resource and the other is to invoke the release Jedis resource when an exception occurs.

The business class, method code is as follows:

  Jedis Jedis = Jedisutils.getjedis ();  
        if (Jedis = = null) {  
            throw new NullPointerException ("Jedis is null");  
        }  
        try{  
            Long queuecurrvalue = Jedis.llen (messagequeuename);  
            if (Queuecurrvalue >= queuemaxsize) {  
                return addflag;  
            }  
            String Serizlizetvalue = json.tojsonstring (message);  
            if (Stringutils.isnotblank (Serizlizetvalue)) {  
                Long queuelength = Jedis.lpush (Messagequeuename, SerizlizetValue );  
                 if (Queuelength-queuecurrvalue > 0) {  
                    Addflag = true;}}  
        } catch (Exception e) {  
            jedisutils.returnbrokenresource (Jedis);  
            Logger.error (E.getmessage (), e);  
        finally{  
            Jedisutils.returnresource (Jedis);  
        }  

See not in the finally{} code block and catch () {} exception called the related method, to free the Jedis resource, so that the previous exception will not occur, of course, the maximum number of links is not set so large, the following look at the modified configuration file

#最大链接数
maxtotal=100
#空闲时最大链接数
maxidle=20
#空闲最小
minidle=8
#链接最大等待时间 (ms)
maxwaitmillis=10000

In this case, there is no more resources to get the exception, the problem is solved, so the problem is not the maximum number of links, but not the release of resources, so no matter how much you set the value will be abnormal and consume a lot of resources.

When I use it in my project, I have the following questions:

        Release resource public
	synchronized void Returnresource (Jedis jedis) {
		if (Jedis! = null) {
			Pool.returnresource ( Jedis);
		}
	}

	Exception free resource public
	synchronized void Returnbrokenresource (Jedis jedis) {
		if (Jedis! = null) {
			Pool.returnbrokenresource (Jedis);
		}
	}

Two methods Returnresource (Jedis), and Returnbrokenresource (Jedis) are painted a line that is obsolete, and still do not know how to replace the method so have to know please advise, thank you first.


Related articles recommended:

Http://www.codeweblog.com/redis%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BA-%E9%85%8D%E7%BD%AE-%E5%8F%8Ajedis %e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95/

http://www.codeweblog.com/jedis%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%85%8D%E7%BD%AE/

http://www.codeweblog.com/jedis-returnresource%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/

http://fengguang0051.iteye.com/blog/2237171

But two of the articles will be more than their own reprint notes ...






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.