【问题标题】:java jedis (redis) cannot connectjava jedis (redis) 无法连接
【发布时间】:2017-09-04 21:54:43
【问题描述】:

我正在尝试在我的 java 应用程序中使用 Jedis 连接到 Redis。我正在实例化一个 JedisPool 对象,当我获取资源时,它会抛出一个异常,说它无法返回资源。奇怪的是,如果我只是实例化一个 Jedis 对象,它就可以毫无问题地连接,而且我可以更改数据。

这是我的 RedisDatabase 类:

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;
    }

}

这是我的错误:

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

【问题讨论】:

  • 你的proxylink是什么,这个异常好像是你的proxylink中的代码造成的
  • @Joel Evans ,您可以在您的项目中使用此替代redission 作为替代
  • @GuangshengZuo 目前,ProxyLink 所做的只是实例化 RedisDatabase 类。所以它没有办法干扰。
  • @kaviranga 我宁愿只使用 Redis。
  • 如果ProxyLink是实例化RedisDatabase类,它是如何实例化RedisDatabase类的?它会调用RedisDatabase(String host, int port, String password) 来构造,对吗?如果是这样,它将一次又一次地执行'ProxyLink.getInstance()'。

标签: java redis connect jedis


【解决方案1】:

您的代码的问题是在 try-with-resource 块中调用 jedis.close()。当块退出时,try-with-resource 块会关闭您的资源。由于您已经关闭了资源,因此在块退出之前,您最终会调用 close 两次。

删除块内对jedis.close 的调用,只使用try-with-resource 功能。

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 2016-10-18
    • 2018-12-29
    • 1970-01-01
    • 2015-10-13
    • 2023-03-10
    • 2020-07-27
    • 2021-10-22
    • 2019-04-04
    相关资源
    最近更新 更多