Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool at redis.clients.jedis.util.Pool.getResource(Pool.java:42) ~[jedis-5.1.2.jar:?] at redis.clients.jedis.JedisPool.getResource(JedisPool.java:378) ~[jedis-5.1.2.jar:?] #3844

Closed
nageswarao-pragada opened this issue May 20, 2024 · 2 comments

Comments

@nageswarao-pragada
Copy link

nageswarao-pragada commented May 20, 2024

As my java application is huge and there thousands of reads and writes will happen per minute, I would like to use JedisPool instead of Jedis.

I have deployed Redis server and My Java application on Kubernetes cluster in the form of Kubernetes POD. My Redis server POD is up and running without any issue.
As my Redis server and Java application is running on same cluster, my application is able to access the redis service using Kubernetes service (redis is the name of my kubernetes service)

Here is the stack track while performing the action in redis

redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
	at redis.clients.jedis.util.Pool.getResource(Pool.java:42) ~[jedis-5.1.2.jar:?]
	at redis.clients.jedis.JedisPool.getResource(JedisPool.java:378) ~[jedis-5.1.2.jar:?]
	at com.nag.redis.store.RedisCacheStore.getCache(RedisCacheStore.java:254) ~[content-manager-17.1.0.jar:?]
	at com.nag.redis.store.RedisCacheStore.get(RedisCacheStore.java:305) ~[content-manager-17.1.0.jar:?]
	at com.nag.redis.store.DatastoreClient.get(DatastoreClient.java:19) ~[content-core-12.0.4.jar:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:?]
	at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: java.lang.IllegalStateException: Pool not open
	at org.apache.commons.pool2.impl.BaseGenericObjectPool.assertOpen(BaseGenericObjectPool.java:770) ~[commons-pool2-2.8.0.jar:2.8.0]
	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:407) ~[commons-pool2-2.8.0.jar:2.8.0]
	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:354) ~[commons-pool2-2.8.0.jar:2.8.0]
	at redis.clients.jedis.util.Pool.getResource(Pool.java:38) ~[jedis-5.1.2.jar:?]
	... 22 more

Here is the my code snippet.

Redis connection initialization

JedisPool jedisPool = null;

private RedisCacheStore( ) {
		initRedisConnection();
}

private void initRedisConnection() {
	try {
		LOG.info("Trying to initiate redis connection.");
                GenericObjectPoolConfig<Jedis> poolConfig = new GenericObjectPoolConfig<>();
	        poolConfig.setMaxTotal(10000); // Set maximum number of connections
	        poolConfig.setMaxIdle(500);  // Set maximum number of idle connections
	        poolConfig.setMinIdle(500);   // Set minimum number of idle connections
	        poolConfig.setTestOnBorrow(true);
	        int timeout = 60000;		
                jedisPool = new JedisPool(poolConfig, "redis", 6379, timeout, "default", "Redis@12345");
		LOG.info("Successfully initiated redis connection.");
	} catch(Exception e) {
		LOG.error("Failed to initialize redis connection. Error : {} {}", e.getMessage(), e);		
       }
}

Get operation code snippet

public String getCache(String cacheName, String key) throws Exception {
	try(Jedis jedis = jedisPool.getResource()) {
	      String jsonString = jedis.get(cacheName);
              Gson gson = new Gson();
              JsonObject jo = gson.fromJson(jsonString, JsonObject.class);
              if (jo != null && jo.has(key)) {
        	   return jo.get(key).getAsString();
               }
	} catch (Exception e) {
	        LOG.error("#Failed to get data : {}", e.getMessage(), e);
	} finally {
	    	if (jedis != null) {
	            jedis.close(); // Close the Jedis resource in the finally block
	        }
	}	   
	return null;
}

Here I am getting exception at jedis.getResource()

Put operation code snippet

public boolean putCache(String storeName, String key, String value) throws Exception {
	try(Jedis jedis = jedisPool.getResource()) {
		String data = jedis.get(storeName);
	        JsonObject jo;
	        if (data == null) {
	            jo = new JsonObject();
	        } else {
	            Gson gson = new Gson();
	            jo = gson.fromJson(data, JsonObject.class);
	        }

	        synchronized (jo) {
	            jo.addProperty(key, value);
	            jedisPool.set(storeName, jo.toString());
	            return true;
	        }
	  } catch (Exception e) {
	        LOG.error("#Failed to put data : {}", e.getMessage(), e);
	        return false;
	    } finally {
	        if (jedis != null) {
	            jedis.close(); // Close the Jedis resource in the finally block
	        }
	    } 
	}
	

Here I am getting exception at jedis.getResource()

Jedis version:
redis.clients jedis 5.1.2
Java version:
java11

@nageswarao-pragada nageswarao-pragada changed the title redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool at redis.clients.jedis.util.Pool.getResource(Pool.java:42) ~[jedis-5.1.2.jar:?] at redis.clients.jedis.JedisPool.getResource(JedisPool.java:378) ~[jedis-5.1.2.jar:?] at com.nag.redis.store.RedisCacheStore.getCache(RedisCacheStore.java:254) ~[content-manager-17.1.0.jar:?] at com.nag.redis.store.RedisCacheStore.get(RedisCacheStore.java:305) ~[content-manager-17.1.0.jar:?] at com.nag.redis.store.DatastoreClient.get(DatastoreClient.java:19) ~[content-core-12.0.4.jar:?] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:?] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:?] at java.lang.Thread.run(Unknown Source) [?:?] May 20, 2024
@nageswarao-pragada nageswarao-pragada changed the title redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool at redis.clients.jedis.util.Pool.getResource(Pool.java:42) ~[jedis-5.1.2.jar:?] at redis.clients.jedis.JedisPool.getResource(JedisPool.java:378) ~[jedis-5.1.2.jar:?] at com.nag.redis.store.RedisCacheStore.getCache(RedisCacheStore.java:254) ~[content-manager-17.1.0.jar:?] at com.nag.redis.store.RedisCacheStore.get(RedisCacheStore.java:305) ~[content-manager-17.1.0.jar:?] at com.nag.redis.store.DatastoreClient.get(DatastoreClient.java:19) ~[content-core-12.0.4.jar:?] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:?] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:?] at java.lang.Thread.run(Unknown Source) [?:?] redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool at redis.clients.jedis.util.Pool.getResource(Pool.java:42) ~[jedis-5.1.2.jar:?] at redis.clients.jedis.JedisPool.getResource(JedisPool.java:378) ~[jedis-5.1.2.jar:?] May 20, 2024
@sazzad16
Copy link
Collaborator

Duplicate #3838

@sazzad16 sazzad16 closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
@sazzad16
Copy link
Collaborator

@nageswarao-pragada First of all, thank you for using Jedis. But please have some patience. You already have #3838. So you don't need to create another post for exactly same purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants