2017-09-04 1 views
1

내 Java 응용 프로그램에서 Jedis를 사용하여 Redis에 연결하려고합니다. JedisPool 객체를 인스턴스화하고 있는데, 리소스를 가져올 때 리소스를 반환 할 수 없다는 예외를 throw합니다. 이상한 것은 비록 Jedis 객체를 인스턴스화하면 문제없이 연결되며 데이터를 변경할 수 있다는 것입니다.java jedis (redis) 연결할 수 없습니다

여기 내 RedisDatabase 클래스의 :

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 
+0

proxylink에서 예외가 발생했습니다. – GuangshengZuo

+0

@Joel Evans, 대체로 [redission] (https://redisson.org/)을 사용할 수 있습니다. – kaviranga

+0

@GuangshengZuo 현재 모든 ProxyLink는 RedisDatabase 클래스를 인스턴스화합니다. 따라서 방해 할 방법이 없습니다. –

답변

0

코드의 문제는 시도 -과 - 자원 블록 내 jedis.close()에 대한 호출입니다 :

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

} 

여기 내 오류입니다. try-with-resource 블록은 블록이 종료 될 때 리소스를 닫습니다. 블록을 종료하기 전에 이미 리소스를 닫았 기 때문에 close를 두 번 호출하게됩니다.

블록 내의 jedis.close에 대한 호출을 제거하고 try-with-resource 기능 만 사용하십시오.

관련 문제