Jedis池被多次初始化

时间:2018-08-01 11:59:38

标签: java redis pool jedis

我正在redis客户的帮助下使用jedis。附上键值设置/获取的代码段。在这里,我期望jedisPool仅被初始化一次,但是它要被初始化多次。不知道我要去哪里错了。用它抓挠我的头几天。我不知道为什么要进行多次初始化。

//$Id$
package experiments.with.truth;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisClientUtil {

    private static JedisPool pool;                  //I persume the deafult value initialised in my static variable would be null
    static int maxActiveConnections = 8;
    static int maxWaitInMillis = 2000;
    static String host = "127.0.0.1";
    static int port = 6379;
    static int REDIS_DB = 1;

    public static void initRedisClient() throws Exception {
        try {
            Class classObj = Class.forName("redis.clients.jedis.JedisPool");
            if (classObj != null && pool == null) {
                JedisPoolConfig jedisConfig = new JedisPoolConfig();
                jedisConfig.setMaxTotal(maxActiveConnections);
                jedisConfig.setMaxWaitMillis(maxWaitInMillis);
                pool = new JedisPool(jedisConfig, host, port);      

                System.out.println("Pool initialised successfully !");
            }
        } catch(ClassNotFoundException ex) {
            System.out.println("Couldn't initialize redis due to unavailability of jedis jar in your machine. Exception : " + ex);
        }
    }
    public Jedis getJedisConnection() {
        if(pool == null) {
            initRedisClient();
        }
        return pool.getResource();
    }
    private static void returnJedis(Jedis jedis) {
        try {
            pool.returnResource(jedis);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    public static String getValue(String key) throws Exception{
        Jedis jedisCon = null; 
        try {
            jedisCon = getJedisConnection();
            jedisCon.select(REDIS_DB);

            String val = jedisCon.get(key); 
            return val;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedisCon != null) {
                returnJedis(jedisCon);
            }
        }
        return null;
    }
    public void addValueToRedis(String key, String value) {
        Jedis jedisCon = null; 
        try {
            jedisCon = getJedisConnection();
            jedisCon.select(REDIS_DB);

            jedisCon.set(key, value); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedisCon != null) {
                returnJedis(jedisCon);
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Value : " + getValue("a"));
        System.out.println("Value : " + getValue("b"));
        System.out.println("Value : " + getValue("c"));
    }
}

我的程序运行时,我可以多次看到此调试日志Pool initialised successfully。有人可以帮我找到这个漏洞吗?或者如何通过在整个程序中仅初始化一次来使它更好(或使其表现出预期的效果)。

1 个答案:

答案 0 :(得分:1)

看起来像一个基本的多线程案例。您的应用要求在短时间内建立5个连接。他们所有人都看到pool == null并进行初始化。

简便的解决方案:public static synchronized void initRedisClient() throws Exception {

更新private static volatile JedisPool pool;,否则您可能会得到空指针异常。

要获取更复杂,性能更高的解决方案,请搜索“ java中的高效懒惰单例”,这很可能会导致您使用Enum解决方案。