jedis icon indicating copy to clipboard operation
jedis copied to clipboard

Allow jedis to automatically connect to given DB index

Open ragnor opened this issue 15 years ago • 10 comments

Currently it's impossible to determinate when Jedis client reconnect to Redis server. If it happens Jedis silently change Redis database index previously set by client (SELECT command) to default 0. Client code is totally unaware of this change and still expect to work with previously selected database. To fix this issue it should be possible to pass an additional parameter databaseIndex (int) to Jedis and Client's constructors. This extra parameter should be used in connect() method to automatically select given database after connecting to Redis server. What do you think about it?

ragnor avatar Feb 03 '11 13:02 ragnor

This might not be ideal for you, but we use Spring-Data and we extended the JedisConnectionFactory class to attain the same result. Looks like this:

   import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory;

   public class StwJedisConnectionPoolFactory extends JedisConnectionFactory 
private static final int DEFAULT_REDIS_DATABASE_INDEX = 0;

private int databaseIndex = 0;

@Override
protected Jedis fetchJedisConnector() {
     Jedis jedis = super.fetchJedisConnector();

     if (this.databaseIndex != DEFAULT_REDIS_DATABASE_INDEX) {
         jedis.select(this.databaseIndex);
     }

     return jedis;
}

public void setDatabaseIndex(int databaseIndex) {
    this.databaseIndex = databaseIndex;
}

Hope this helps (or can help in the short term)

ensonik avatar Feb 03 '11 14:02 ensonik

I think your solution is incorrect. You assume that between fetching jedis client from factory (calling fetchJedisConnector method) and using this client in you code nothing bad with connection to Redis server happen. This assumption is wrong. It may happen that you fetch Jedis client and select the database that you want but before you use it jedis reconnect to REdis server because of some network's problems ... and automatically connect to database 0, without notifying you about this.


StwJedisConnectionPoolFactory  factory = new StwJedisConnectionPoolFactory();
factory.setDatabaseIndex(2);
String key = "my-nice-key";
Jedis client = factory.fetchJedisConnector();
..........
// at this point due to networks problem Jedis client has lost connection to Redis server
client.get(key); // invoking this method will cause the client to connect again to Redis server but not to database 2 but to default 0

I hope I expressed it clearly.

ragnor avatar Feb 03 '11 16:02 ragnor

What do you mean by reconnecting? Right now Jedis doesn't reconnect on network errors, It will just throw an exception.

xetorthio avatar Feb 04 '11 11:02 xetorthio

As far as I understand code in Jedis and Client classes, invocation of most of the methods is preceded with Jedis.connect() public void connect() throws UnknownHostException, IOException { if (!client.isConnected()) { client.connect(); if (this.password != null) { this.auth(this.password); } } } which calls Client.isConnected() public boolean isConnected() { return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown(); }

and if false is returned the connection is automatically established to default database 0. I prefer the client to connect each time to database that I've configured (other than default 0). It'll be a nice feature. I wonder what will happen if Jedis object isn't used for a long time and Redis server closes the connection or other thing happen which cause isConnected to return false? jedis.select(2); jedis.set("key", "value") jedis.get("key"); Thread.sleep(300 * 1000); // let's assume that Redis server close this connection try { jedis.get("key"); // it'll throw exception or connect again because isConnected return false? } catch (Exception ex) { ex.printStackTrace(); }

I've check it and exception is thrown. So is it any way to cause isConnected to return false after client has established connection?

ragnor avatar Feb 04 '11 14:02 ragnor

This is a good question. I actually couldn't find a way to detect if the server closed the connection without executing some command. So I think it might not be possible, but I'm not 100% sure.

xetorthio avatar Feb 07 '11 13:02 xetorthio

We just added support for this in trunk in Spring Data. However it would work better if the Jedis would support this at its core because with connection pooling for example, we need to set the database each time a connection is fetched since we cannot differentiate between an already pooled connection and a newly created one. Opening up JedisFactory (based on BasePoolableObjectFactory) or supporting this directly in Jedis would make things easier and more performant.

costin avatar Mar 14 '11 09:03 costin

Hi! Regard this I see 2 different things here. One is when I do jedis.select(2); and for some reason the socket is unbound or something like that. What will happen is that it will try to reconnect to Redis and won't select the right db and that is what ragnor is saying (I think).

Another thing is what costin is suggesting. Which is being able to do something like new JedisPool(host, port, ..., db); So every resource that the pool deliver is already selected to the correct db (again I think you said that :) ).

So regard the first one. Can you reproduce a situation where that happens? I need to be able to produce an error like that so I can fix it.

Regard the second. I think not many users need something like this, specially because more than 1 db is discouraged and will be deprecated. But if people say they need it, we can implement it! :)

xetorthio avatar May 12 '11 01:05 xetorthio

Hi! I cannot produce this kind of an error. I've created this issue not because I'd noticed this problem on my servers. After analysing jedis' sources I'd realized that such an error may occur.
To avoid such possible error I've created wrapper around redis.clients.jedis.Client and override connect method to call select if the database number is different from 0. This solution has been working correctly for few months.

ragnor avatar May 13 '11 06:05 ragnor

Ok, I will try to produce the error for the sake of testing. I will add this to 2.0.0 :)

xetorthio avatar May 13 '11 10:05 xetorthio

done and pushed to master!

xetorthio avatar May 16 '11 01:05 xetorthio