问题描述
- redis jedis jedispool 获取不到连接
-
每次都在第8次获取 jedis的时候 获取不到package com.ryx.sync.util; import java.util.concurrent.locks.ReentrantLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public final class JedisUtil { private static Logger logger = LoggerFactory.getLogger(JedisUtil.class); protected static ReentrantLock lockPool = new ReentrantLock(); protected static ReentrantLock lockJedis = new ReentrantLock(); //Redis服务器IP private static String ADDR = "127.0.0.1"; //Redis的端口号 private static int PORT = 6379; //访问密码 private static String AUTH = "beijing"; //可用连接实例的最大数目,默认值为8; //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 private static int MAX_ACTIVE = 1024; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 private static int MAX_IDLE = 8; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; private static int MAX_WAIT = 10000; private static int TIMEOUT = 10000; //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null; /** * 初始化Redis连接池 */ private static void initialPool(){ try { JedisPoolConfig config = new JedisPoolConfig(); // config.setMaxTotal(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT); } catch (Exception e) { logger.error("create JedisPool error : "+e); } } /** * 在多线程环境同步初始化 */ private static void poolInit() { //断言 ,当前锁是否已经锁住,如果锁住了,就啥也不干,没锁的话就执行下面步骤 assert ! lockPool.isHeldByCurrentThread(); lockPool.lock(); try { if (jedisPool == null) { initialPool(); } }catch(Exception e){ e.printStackTrace(); } finally { lockPool.unlock(); } } /** * 获取Jedis实例 * @return */ public static Jedis getJedis() { //断言 ,当前锁是否已经锁住,如果锁住了,就啥也不干,没锁的话就执行下面步骤 assert ! lockJedis.isHeldByCurrentThread(); lockJedis.lock(); if (jedisPool == null) { poolInit(); } Jedis jedis = null; try { if (jedisPool != null) { jedis = jedisPool.getResource(); } } catch (Exception e) { logger.error("Get jedis error : "+e); }finally{ lockJedis.unlock(); } return jedis; } /** * 释放jedis资源 * @param jedis */ public static void returnResource(final Jedis jedis) { if (jedis != null && jedisPool !=null) { jedisPool.returnResource(jedis); } } public static JedisPool getJedisPool(){ return jedisPool; } } public boolean jedisSave(List<SdTranData> sdTranDatas){ if (sdTranDatas.size() <= 0) { return false; } Jedis jedis = null; try { //从redis 删除边界 tranCode 解决重复问题 this.logger.logDebug("正在获取jedis连接。。。"); jedis = JedisUtil.getJedis(); System.out.println("active jedis:"+JedisUtil.getJedisPool().getNumActive()); this.logger.logDebug("获取jedis连接成功。。。"); String tranCode = sdTranDatas.get(0).getTranCode(); jedis.zremrangeByScore("sdTranLs".getBytes(), Double.parseDouble(tranCode), Double.parseDouble(tranCode)); for (SdTranData sdTranData : sdTranDatas) { jedis.zadd("sdTranLs".getBytes(), Double.parseDouble(sdTranData.getTranCode()), SerializeUtil.serialize(sdTranData)); } }catch (Exception e) { logger.logException("获取jedis连接出错:", e); }finally{ if(jedis != null){ JedisUtil.returnResource(jedis); } } return true; }
解决方案
程序其他地方 获取了连接,没关闭
时间: 2024-09-22 11:12:40