18

I'm having problems completing an .hgetall(), here's what I've tried:

Jedis jedis = new Jedis(REDIS_MASTER_NODE);
jedis.connect();
jedis.configSet("timeout", "30");

Map<String, String> alreadyStored = jedis.hgetAll(redisTargetHash);

and here's what I get:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
    at redis.clients.jedis.Protocol.process(Protocol.java:79)
    at redis.clients.jedis.Protocol.read(Protocol.java:131)
    at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:199)
    at redis.clients.jedis.Jedis.hgetAll(Jedis.java:851)

This solved the issue:

Jedis jedis = new Jedis(REDIS_MASTER_NODE, 6379, 1800);
Shubham
  • 2,847
  • 4
  • 24
  • 37
dranxo
  • 3,348
  • 4
  • 35
  • 48

3 Answers3

23

If what you want to do is set Jedis connection timeout, you should do it using the special constructor made for that:

public Jedis(final String host, final int port, final int timeout)

What you are doing is setting the timeout on Redis settings from Jedis. Doing CONFIG SET timeout 60, means that Redis will close idle client connections after 60 seconds. That's why you get the exception in Jedis.

informatik01
  • 16,038
  • 10
  • 74
  • 104
xetorthio
  • 1,304
  • 10
  • 9
  • 14
    Whats the unit of timeout? As in, is it in seconds or milliseconds? – abi_pat Jul 23 '15 at 15:05
  • @xetorthio can you tell me how I can specify query timeout in jedis ? Currently I am using `jedis-2.6.0.jar` Please also tell me what are default values of them. As per my understanding `connectionTimeout` is new connection establishing timeout. – Black_Rider Aug 04 '15 at 12:03
  • 9
    The timeout parameter of the constructor is in milliseconds because this value is affected internally to java.net.Socket#connect(java.net.SocketAddress, int) and java.net.Socket#setSoTimeout(int) methods. So this this value is connection and a socket read timeout. – Stéphane B. Sep 01 '15 at 07:22
12

This is a bit of an extension to xetorthio's answer, but here is similar approach for use with a JedisPool. (Caveat: this is based on my understanding from looking at the Jedis version 2.6.2 code directly and has not been tested in a live use case.)

    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxWaitMillis(writeTimeout);
    JedisPool pool = new JedisPool(jedisPoolConfig, redisHost, port, readTimeout);

The writeTimeout is max time for a Jedis resource from the pool to wait for a write operation.

The readTimeout specified for the pool constructor is the wait time for a socket read, see java.net.Socket.setSoTimeout for more specific details.

being_ethereal
  • 795
  • 7
  • 26
Paul-59701
  • 187
  • 1
  • 3
  • 3
    JedisPoolConfig inherits maxWaitMillis from BaseGenericObjectPool , its just a timeout for getting/borrowing the object from JedisPoolConfig https://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/BaseGenericObjectPool.html#getMaxWaitMillis-- I dont think it has anything to do with writetimeout of remote redis – Manas Saxena Jan 21 '19 at 20:48
  • maxWaitMillis -: The maximum wait time (in milliseconds) of the caller when the connection pool resource is exhausted. – Kumar-Sandeep Mar 04 '21 at 08:41
4

Few things to consider:

  1. For both Jedis and JedisPool classes, timeout is in miliseconds. Default timeout, at least in 2.5.1, as I see, is 2000 (milisec): int redis.clients.jedis.Protocol.DEFAULT_TIMEOUT = 2000 [0x7d0]

  2. As per this documentation, Redis 2.6 or higher does not close connection, even if the client is idle. I have not verified this yet, and I will try to update the post when I do.

Erica Kane
  • 3,137
  • 26
  • 36
milkyway
  • 53
  • 8
  • did you get a chance to see if the connection is closed after the idle period timeout? Also, I guess you're saying Jedis (not Redis) doesn't close. – asgs Apr 05 '18 at 13:27
  • It is Redis 2.6 and higher that will leave client connections open by default. – Erica Kane Feb 05 '19 at 12:57