When you use JedisPool, the following error may occur, indicating the failure to obtain a resource from the pool.
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
You can solve this issue in the following ways.
Check the network
First, check the network condition. You can test the network by running the telnet host 6379
command. After the host is connected, input a combination of auth, a space and a password
, and then press ENTER. If the system returns +OK\r\n
, run the Ping
command or read and write requests to check whether the system indicates a successful
operation. Perform multiple test operations to check the network conditions.
Check the setting of JedisPool connections
You must set JedisPool connections when using the connection pool. A failure to obtain
a resource from the pool may occur when the number of connections is more than the
value of MaxTotal. In this case, you can run netstat -an | grep 6379 | grep EST | wc -l
on the client to view the number of connections and compare the result with the value
of MaxTotal. If the two values are close to each other, the setting of the connection
pool is correct.
Check JedisPool code
When you use JedisPool, after the getResource
operation, you must call the returnResource
or close
operation to return the resource. You can check whether the operation code is correct.
The example code is as follows:
JedisPoolConfig config = new JedisPoolConfig();
//The maximum number of idle connections. You can set the parameter for your application as needed. The value of this parameter cannot be more than the maximum number of clients connected to each ApsaraDB for Redis instance.
config.setMaxIdle(200);
//The maximum number of connections. You can set the parameter for your application as needed. The value of this parameter cannot be more than the maximum number of clients connected to each ApsaraDB for Redis instance.
config.setMaxTotal(300);
config.setTestOnBorrow(false);
config.setTestOnReturn(false);
String host = "*.aliyuncs.com";
String password = "Password";
JedisPool pool = new JedisPool(config, host, 6379, 3000, password);
Jedis jedis = null;
try {
jedis = pool.getResource();
/// ... do stuff here ... for example
jedis.set("foo", "bar");
String foobar = jedis.get("foo");
jedis.zadd("sose", 0, "car");
jedis.zadd("sose", 0, "bike");
Set<String> sose = jedis.zrange("sose", 0, -1);
} finally {
if (jedis ! = null) {
jedis.close();
}
}
/// ... when closing your application:
pool.destroy();
Check for nf_conntrack packet loss
Run the dmesg
command to check whether the client has any exceptions.
nf_conntrack: table full, dropping packet
If nf_conntract packet loss occurs, modify the setting sysctl -w net.netfilter.nf_conntrack_max=120000
.
Check TIME_WAIT connections
Run the ss -s
command to check whether excessive TIME_WAIT connections exist.
If so, modify the following parameters:
sysctl -w net.ipv4.tcp_max_tw_buckets=180000
sysctl -w net.ipv4.tcp_tw_recycle=1
Check DNS resolution
Bind the host address in the /etc/hosts file and then check whether the issue persists. If so, DNS resolution is normal.
192.168.1.1 *.redis.rds.aliyuncs.com
Help
sudo tcpdump -i eth0 tcp and port 6379 -n -nn -s 74 -w redis.cap