skip to Main Content

While trying to use Jedis.get(key) while accessing the Redis using Jedis I’m getting this error.

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)

The code to access the Redis looks like below:

 private static JedisPoolConfig buildPoolConfig() {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(128);
    poolConfig.setMaxIdle(128);
    poolConfig.setMinIdle(16);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(true);
    poolConfig.setTestWhileIdle(true);
    poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
    poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
    poolConfig.setNumTestsPerEvictionRun(3);
    poolConfig.setBlockWhenExhausted(true);
    return poolConfig;
  }

this.jedisPool = new JedisPool(poolConfig, "localhost", 6379, 4000);
this.jedis = jedisPool.getResource();


//Now using this jedis connection to retrieve values for key
jedis.get(key)  // error occurs

3

Answers


  1. Chosen as BEST ANSWER

    The solution was to create Jedis instance as below:

     try (Jedis jedis = jedisPool.getResource()) { 
         // do get here
      }
    

    This worked for me! Things I tried and can come in handy for others based on what I found:

    1. Try changing the Redis jar version.
    2. Check the logs and if you see some error related to protected mode you may want to switch it off.
    3. Check if your Redis cluster/instance is up and running.

  2. This sort of error might occur when the (JedisPool) resource becomes exhausted.

    Try to wrap in a try-catch-finally block and in the finally block return the jedis resource to the pool.

    Example illustrating relevant block:

    try {
      
      jedis = jedisPool.getResource();
      String value = jedis.get(key);
      //do something with value
    
    } catch (Throwable e) {
    
      //handle exception
    
    } finally {
      
      if (jedis != null) {
        //free the jedis resource
        jedisPool.returnResource(jedis);
      }
    
    }
    
    Login or Signup to reply.
  3. Check whether your Redis server is online. And to prevent exhaustion, always close() you jedis instances collected from JedisPool.

    Also check whether you need more than 200 (DEFAULT MAX TOTAL Connections)

        try {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(1000); // set max here
                jedisPool = new JedisPool(config, redisHost, redisPort, 10000);     
            }catch(Exception e){
                System.out.println("have error while creating jedisPool " + e);
            }
    

    and when you use jedis, use it as follows :

    func(){
           Jedis userTemplate = null;
            try {
                jedis = jedisPool.getResource();
                // do stuff here
                // jedis.get("hi-there");
            }catch(Exception e) {
                System.out.println("error in getting resource " + e);
            }finally {
                if(userTemplate != null) {
                    userTemplate.close();
                }
            }
        }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search