Redis does not release problems when using connections in the app to resolve

Source: Internet
Author: User

Today's test shows that when Redis is used, the link to the call is not released. After looking at pretty much data, only to find a configuration caused. It's not that they say the service didn't start.

1) configuration file

#redis连接配置 ===================start=========================# Redis settingsredis.host=192.168.10.102 redis.port=6379redis.pass=redis.maxidle=1redis.maxactive =9  Redis.maxwait=1000redis.testonborrow=true#redis连接配置===================end===== ====================

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "http://Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id= "Poolconfig"class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "maxidle" value= "${redis.maxidle}"/> &LT;PR Operty name= "Maxtotal" value= "${redis.maxactive}"/> <property name= "Maxwaitmillis" value= "${redis.maxWait}" /> <property name= "Testonborrow" value= "${redis.testonborrow}"/> </bean> <bean id= "JedisCo Nnectionfactory "class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name= "poolconfig" ref= "Poolconfig"/> <property name= "port" value= "${redis.port}"/> <property name= "HostName" Val Ue= "${redis.host}"/> <property name= "password" value= "${redis.pass}"/> </bean> <be An id= "Stringserializer"class= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id= " Jdkserializationredisserializer "class= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <bean id= " Stringredistemplate "class= "Org.springframework.data.redis.core.StringRedisTemplate" > <property name= "connectionfactory" ref= " Jedisconnectionfactory "/> <property name=" Keyserializer "ref=" Stringserializer "/> <property na Me= "ValueSerializer" ref= "Jdkserializationredisserializer"/> <property name= "EnableTransactionSupport" value = "true"/> </bean> <bean id= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate" > <property name= "connectionfactory" ref= " Jedisconnectionfactory "/> <property name=" Keyserializer "ref=" Stringserializer "/> <property na Me= "ValueSerializer" ref= "Jdkserializationredisserializer"/> </bean></beans>

2) Test Examples

A SPRINGMVC controller class was written, and then the calling thread used the connection and there was a problem.

Demomvccontroller.java

 PackageCom.iafclub.demo.web.controller;Importjavax.servlet.http.HttpServletRequest;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.StringRedisTemplate;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;Importcom.iafclub.baseTools.util.MyDateUtil; @Controller Public classDemomvccontroller {@AutowiredPrivatestringredistemplate stringredistemplate; /*** Jump Mode 3 **/@RequestMapping ("/testredis.do")     Public voidTestredis (model model, HttpServletRequest request) {System.out.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");  for(inti=0;i<5;i++) {thread thread=NewRedisthread (stringredistemplate); Thread.setname ("Thread:" +i);        Thread.Start (); } model.addattribute ("Status", "Done" +mydateutil.getcurrentdatetimestr ()); System.out.println (Complete); System.out.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); }}

Redisthread.java Threading Class

 PackageCom.iafclub.demo.web.controller;ImportOrg.junit.runner.RunWith;Importorg.springframework.data.redis.core.BoundValueOperations;Importorg.springframework.data.redis.core.StringRedisTemplate;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportCom.iafclub.baseTools.util.MyDateUtil; the @RunWith (Springjunit4classrunner.class) Public classRedisthreadextendsThread {Privatestringredistemplate redistemplate; PrivateString Reverse_key = "Batchjob:task_";  PublicRedisthread (stringredistemplate redistemplate) { This. redistemplate =redistemplate; } @Override Public voidrun () {//It's actually used many times, but it's also a link .         for(inti=0;i<50;i++) {String value= Thread.CurrentThread (). GetName () + "{User:user" +mydateutil.getcurrentdatetimestr () + "; Name:chenweixian" + System.currenttimemillis () + "}"; Redistemplate.opsforvalue (). Set (Reverse_key+System.currenttimemillis (), value); Redistemplate.getconnectionfactory (). getconnection (). Close ();//boundvalueoperations<string, string> opt = Redistemplate.boundvalueops (reverse_key+system.currenttim Emillis ());//Opt.set (value);//System.out.println (Opt.get ());        }System.out.println (Complete); }    }

Launch app, Access link: http://chenweixian-pc:8480/demo-system/testRedis.do, more refresh several times

Problem exception: Cannot get Jedis connection

Exception in Thread "Thread: 3"org.springframework.data.redis.RedisConnectionFailureException:Cannot get Jedis connection; nested exception is Redis.clients.jedis.exceptions.JedisConnectionException:Could not get a resource from the pool at org.springframework. Data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector (Jedisconnectionfactory.java:162) at Org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection ( Jedisconnectionfactory.java:251) at Org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection ( Jedisconnectionfactory.java:58) at Com.iafclub.demo.web.controller.RedisThread.run (Redisthread.java:26) caused by:redis.clients.jedis.exceptions.JedisConnectionException:Could not get a resource from the pool at Redi S.clients.util.pool.getresource (Pool.java:50) at Redis.clients.jedis.JedisPool.getResource (Jedispool.java:88) at Redis.clients.jedis.JedisPool.getResource (Jedispool.java:12) at Org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector ( Jedisconnectionfactory.java:155)    ... 3morecaused by:java.util.NoSuchElementException:Timeout Waiting forIdle object at Org.apache.commons.pool2.impl.GenericObjectPool.borrowObject (Genericobjectpool.java:442) at Org.apache.commons.pool2.impl.GenericObjectPool.borrowObject (Genericobjectpool.java:360) at Redis.clients.util.Pool.getResource (Pool.java:48)    ... 6 more

3) View the number of links

The client tool to the server to query the current number of connections: the current 10

[Email protected] bin]#./redis-CLI Info clients# clientsconnected_clients:client_longest_output_ List:0client_biggest_input_buf:0blocked_clients:0

4) Analyzing the problem

Because we set the initial number of connections the maximum is 9, plus my own through the Client Access connection Number 10, theoretically should be released, there is no release, there is a problem. Because this link should be the same as the database link, will be released, in order to long ...

A long interval of visits, still 10. Not released. Once a HttpRequest request is sent, the error is still: No link has been taken.

Exception in Thread "thread: 3" Org.springframework.data.redis.RedisConnectionFailureException:Cannot get Jedis Connection ; Nested exception is redis.clients.jedis.exceptions.JedisConnectionException:Could does get a resource from the pool

5) Modify the configuration

After repeatedly looking for properties, finally found a configuration in the configuration file, is transacted, online query learned that if you start the transaction management in Redis, you must use Mul and execute to take effect. And we're not using this business here. So remove this configuration.

    class= "Org.springframework.data.redis.core.StringRedisTemplate" >        <property name= " ConnectionFactory "ref=" jedisconnectionfactory "/>        <property name=" Keyserializer "ref=" StringSerializer " />        <property name= "ValueSerializer" ref= "Jdkserializationredisserializer"/>        <property Name= "Enabletransactionsupport" value= "true"/>    </bean>

6) Re-test

It is normal that the number of connections cannot be obtained after redeployment, startup, and multiple flushes.

# clientsconnected_clients:  1 client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0

7) Problem Solving

Summary: This configuration item needs to be noted.

Redis does not release problems when using connections in the app to resolve

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.