redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted

When do JedisPool really a lot of problems encountered into the next question, here is a solution, not necessarily use all of encounter, because there may be a lot of people are using the redis configured in the virtual machine, I use the configuration is redis on centos server, possible causes of the problem is from here, because if it is local, then, redis.conf which is the default configuration bind 127.0.0.1, and remote configuration connection if you want to comment out the sentence, and the default non-local connections into protected mode? We need to set a password or modify other configurations to achieve the purpose of the visit.

For example, the following tips

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:

1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
    
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
    
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
    
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

 

Minor resolve this error:

redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
    at redis.clients.jedis.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:234)
    at com.jedis.jedistest.PoolTest.main(PoolTest.java:15)
Caused by: java.util.NoSuchElementException: Unable to validate object
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:479)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:346)
    at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
    ... 2 more

 

Baidu Google to find solutions do not apply, then thought, probably because they did not set a password for remote access when due? Just to see the discussion here, https://www.oschina.net/question/579073_113004

So will poolConfig.setTestOnBorrow (false); from the original true to false, and to set a password redis 123, can be accessed.

 

 

Original video link given below: https://www.bilibili.com/video/av51989396/?p=28

Code is shining inside knock, but different versions of some packages, some methods have changed.

JedisPoolUtil.java

Package com.jedis.jedistest; 

Import redis.clients.jedis.Jedis;
 Import redis.clients.jedis.JedisPool;
 Import redis.clients.jedis.JedisPoolConfig; 

// Double Lock Chcek 
public  class JedisPoolUtil { 

    Private  static  volatile JedisPool jedisPool = null ; 

    // Constructors privatization 
    Private JedisPoolUtil () { 
    } 

    // returned to the pool by a method of example 
    public  static jedisPool getJedisPoolInstance () { 

        IF ( null == jedisPool) {
             // lock object
            synchronized (JedisPoolUtil.class) {
                if (null == jedisPool) {

                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    // poolConfig.setMaxTotal(1000);
                    poolConfig.setMaxTotal(1000);
                    poolConfig.setMaxIdle(32);
                    poolConfig.setMaxWaitMillis(100 * 1000);
                    poolConfig.setTestOnBorrow(false);
                //    poolConfig.setTestOnBorrow(true);

                    jedisPool = new JedisPool(poolConfig, "233.233.223.223", 6666);
                }
            }
        }

        return jedisPool;

    }

    public static void release(JedisPool jedispool,Jedis jedis) {
        if(null!=jedis) {
            //jedisPool.returnResource(jedis) ---> jedis.close();
            //升级版的jedis用close来替代returnResource?
            //jedispool.getResource();
            jedis.close();
        }
    }
}

 

PoolTest.java

 1 package com.jedis.jedistest;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 
 6 public class PoolTest {
 7 
 8     public static void main(String[] args) {
 9     
10         JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();
11         Jedis jedis = null;
12             
13         
14         try {
15             jedis = jedisPool.getResource();
16             jedis.auth("123");
17             jedis.set("k1", "siyi");
18         } catch (Exception e) {
19             e.printStackTrace();
20         }finally {
21             JedisPoolUtil.release(jedisPool, jedis);
22         }
23 
24     }
25 
26 }

 

Guess you like

Origin www.cnblogs.com/Guhongying/p/11222314.html