2

I'm trying to connect to Redis using Jedis in my java application. I'm instantiating a JedisPool object, and when I get the resource, it throws an exception saying it cannot return the resource. What is weird though is if I just instantiate a Jedis object, it connects without problems, and I can change data.

Here's my RedisDatabase class:

package me.joeleoli.proxylink.database; import me.joeleoli.proxylink.ProxyLink; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class RedisDatabase { private JedisPool pool; public RedisDatabase(String host, int port, String password) { ProxyLink.getInstance().getLogger().info("Attempting to establish Redis connection " + host + ":" + port); this.pool = new JedisPool(host, port); try (Jedis jedis = this.pool.getResource()) { if (password != null && !password.equals("")) { jedis.auth(password); } jedis.select(0); jedis.close(); } } public JedisPool getPool() { return this.pool; } } 

Here's my error:

22:16:15 [INFO] [ProxyLink] Attempting to establish Redis connection 127.0.0.1:6379 22:16:15 [WARNING] Exception encountered when loading plugin: ProxyLink redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:106) at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:12) at redis.clients.jedis.Jedis.close(Jedis.java:3206) at me.joeleoli.proxylink.database.RedisDatabase.<init>(RedisDatabase.java:23) at me.joeleoli.proxylink.ProxyLink.onEnable(ProxyLink.java:71) at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:227) at net.md_5.bungee.BungeeCord.start(BungeeCord.java:273) at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:111) at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15) Caused by: redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool at redis.clients.util.Pool.returnResourceObject(Pool.java:61) at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:103) ... 8 more Caused by: java.lang.IllegalStateException: Object has already been returned to this pool or is invalid at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:551) at redis.clients.util.Pool.returnResourceObject(Pool.java:59) ... 9 more 
6
  • What does your proxylink, The exception seems like caused by your code in proxylink Commented Sep 4, 2017 at 2:44
  • @Joel Evans , you can use this alternative redission for your project as an alternative Commented Sep 4, 2017 at 2:46
  • @GuangshengZuo at the moment, all ProxyLink does is instantiate the RedisDatabase class. So there's no way it interferes. Commented Sep 4, 2017 at 3:02
  • @kaviranga I'd rather just use Redis. Commented Sep 4, 2017 at 3:02
  • if ProxyLink does is instantiate the RedisDatabase class, how it instantiate RedisDatabase class? It will call RedisDatabase(String host, int port, String password) to construct, right? If so, it will execute 'ProxyLink.getInstance()' again and again. Commented Sep 4, 2017 at 3:18

1 Answer 1

1

The problem with your code is the call to jedis.close() within the try-with-resource block. The try-with-resource block with closes your resource when the block exits. Since you already closed the resource, prior to the block exiting, you end up calling close twice.

Remove the call to jedis.close within the block and just use the try-with-resource functionality.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.