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
The solution was to create Jedis instance as below:
This worked for me! Things I tried and can come in handy for others based on what I found:
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:
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)
and when you use jedis, use it as follows :