JAVA整合Redis使用连接池遇见Could not return the resource to the pool问题

原因:自己粗心犯了很低级错误将Jedis和JedisPool对象跟常用属性设置成全局static造成后面的Jedis不是从连接池中获取的,造成关闭错误,并且引发后面使用Jedis对象还有之前的数据缓存在反序列化数据时也会有io异常

解决将静态全局变量改成局部变量!

问题解决完,才来进行记录的,告诫以后的自己,细心!!!--附上整个代码

//创建连接池对象
    private static JedisPool pool=null;
    //设定连接超时时间
    private static int POOL_TIMEOUT = 10000;
    //创建连接池
    public static JedisPool getPool(String host,int port,String pwd){
        if(pool==null ){
            JedisPoolConfig config = new JedisPoolConfig();
            //设置jedis的最大实例数
            config.setMaxTotal(100);
            //设置jedis的最大空闲实例数
            config.setMaxIdle(5);
            // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
            config.setMaxWaitMillis(1000 * 10);
            // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
            config.setTestOnBorrow(true);
 
            pool=new JedisPool(config,host,port,POOL_TIMEOUT,pwd);
        }
        return pool;
    }
 
 
    //关闭连接池
    public static void closePool(){
        if(pool!=null){
            pool.close();
        }
    }
 
    //归还jedis实例到连接池
    public static void returnJedis2Pool(Jedis jedis){
        if(jedis!=null){
        	try {
        		jedis.close();
			} catch (Exception e) {
				System.out.println("释放jedis资源出错,将要关闭jedis,异常信息:" + e.getMessage());
				if (jedis != null) {
                    try {
                        // 2. 客户端主动关闭连接
                        jedis.disconnect();
                    } catch (Exception e1) {
                    	System.out.println("disconnect jedis connection fail: " + e.getMessage());
                    }
                    }
			}
            
        }
    }
    
    //将对象转成字节数组
    public static byte[] sessionToByte(Session  session){
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        byte[] bytes = null;
        try {
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(session);
            bytes = bo.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }
    
    //将字节数组转成对象
    public static Object toObject (byte[] bytes) {      
        Object obj = null;
        try {        
            ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
            ObjectInputStream ois = new ObjectInputStream (bis);
            obj = ois.readObject();
            ois.close();   
            bis.close();
        } catch (IOException ex) {        
            ex.printStackTrace();   
        } catch (ClassNotFoundException ex) {        
            ex.printStackTrace();   
        }      
        return obj;    
    }

	
	public static void main(String[] args) {
		String host="127.0.0.1";
        int port=6379;
        String pwd=null;
        long l1=System.currentTimeMillis();
        for(int i=0;i<100000;i++) {
            JedisPool pool = null;
            Jedis jedis=null;
            try {
                pool = getPool(host, port, pwd);
                jedis = pool.getResource();
                jedis.select(1);
                jedis.set(i + "a", i + "b");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                returnJedis2Pool(jedis);
            }
        }
        closePool();
        long l2=System.currentTimeMillis();
        System.out.println("已经完成,用时:"+(l2-l1));
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值