Redis uses connection pool to solve the problem of error reporting

Redis uses the connection pool to report errors
redis has been reporting exceptions for more than ten hours

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool  
    at redis.clients.util.Pool.getResource(Pool.java:22)  
    at com.derbysoft.jredis.longkeytest.BorrowObject.run(BorrowObject.java:22)  
    at java.lang.Thread.run(Thread.java:662)  
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object  
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1134)  
    at redis.clients.util.Pool.getResource(Pool.java:20)  

The reason is that there is no recycling of resources, resulting in
the correct approach is

ShardedJedis jedis = shardedJedisPool.getResource();
try {
    jedis.set(key, value);
} finally {
    shardedJedisPool.returnResource(jedis);
}

 

Update content:

If you use jedis 2.4.2 or earlier, don’t forget to return and connect to the resource pool

However, after version 2.5.0, jedis uses try with resource. When jedis runs out, it will be returned automatically. You don’t have to return every time

The parameters of various link numbers can be adjusted by yourself. For example, maxtotal, maxidle, etc~

If the redis server is not in the LAN, the link may timeout due to network problems, and a socket exception will be thrown. Of course, the write data will also be lost

The connection timeout is 2000ms by default. You can adjust it larger by yourself, so that data will not be lost when the network is slow

A very important point: during development, method a calls method B. method a needs to obtain jedis links, and method B also needs to obtain jedis links. During implementation, try to make them public connections. For example, B uses the link of a, so that efficiency will be much faster and links will not be wasted

the most important thing is to answer from Google foreigners:

1.I test it using a multi-thread code, when the poolSize > maxclients(redis.conf),
it will throw this exception. And it runs well when the maxclients is larger than poolSize.

2.If you set maxclients to 10, and set fixed thread pool size to 20, it prints “.. fail to get connection…”.
If you set maxclients to 10, and set fixed thread pool size to 5, it runs well.

 

Thus, in the redis configuration file and redis.conf file, there is a parameter setting, maxclients (commented out by default. If this value is not set, there is no limit on the number of redis client connections)

When setting, the value of maxclients should be greater than poolsize (the maximum number of links in the connection pool)

 

Similar Posts: