/// <summary> /// 开启监视 /// </summary> private void Start() { Thread idle = new Thread(() => { PoolEntry poolEntry = null; while (!IsStop) { Thread.Sleep(idleTimeOut); int num = idleTimeQueue.Count; long now = DateTime.Now.Ticks; while (num > 0) { if (idleTimeQueue.TryDequeue(out poolEntry)) { //超过空闲时间就不需要,标记移除 if ((now - poolEntry.AccessedTime) / TickMS > idleTimeOut) { poolEntry.CompareAndSetState(IConcurrentBagEntry.STATE_NOT_IN_USE, IConcurrentBagEntry.STATE_REMOVED); } num--; if (poolEntry.State != IConcurrentBagEntry.STATE_REMOVED) { //已经标记移除的不再监测 idleTimeQueue.Enqueue(poolEntry); } } } } }); idle.Name = PoolName + "_idle"; idle.IsBackground = true; idle.Start(); // Thread maxLeft = new Thread(() => { PoolEntry poolEntry = null; while (!IsStop) { Thread.Sleep(maxLeftTime); int num = maxLiveQueue.Count; long now = DateTime.Now.Ticks; while (num > 0) { if (maxLiveQueue.TryDequeue(out poolEntry)) { if ((now - poolEntry.CreateTime) / TickMS > maxLeftTime) { poolEntry.CompareAndSetState(IConcurrentBagEntry.STATE_NOT_IN_USE, IConcurrentBagEntry.STATE_REMOVED); } num--; if (poolEntry.State != IConcurrentBagEntry.STATE_REMOVED) { //已经标记移除的不再监测 maxLiveQueue.Enqueue(poolEntry); } } } } }); maxLeft.Name = PoolName + "_left"; maxLeft.IsBackground = true; maxLeft.Start(); // Thread leakDetection = new Thread(() => { PoolEntry poolEntry = null; int cout = 10;//延迟10s没有设置就退出; while (!IsStop) { Thread.Sleep(leakDetectionThreshold); if (leakDetectionThreshold == 0) { Thread.Sleep(1000);//延迟1s; cout--; if (cout == 0) { break; } continue; } int num = userQueue.Count; long now = DateTime.Now.Ticks; while (num > 0) { if (userQueue.TryDequeue(out poolEntry)) { if (poolEntry.State == IConcurrentBagEntry.STATE_IN_USE) { if ((now - poolEntry.AccessedTime) / TickMS > leakDetectionThreshold) { Logger.Singleton.Warn(string.Format("{0}-可能泄露,实体:{1}", PoolName, poolEntry.ID)); } } num--; if (poolEntry.State == IConcurrentBagEntry.STATE_IN_USE) { //没有使用的不再监测 userQueue.Enqueue(poolEntry); } } } } }); leakDetection.Name = PoolName + "_leakDetection"; leakDetection.IsBackground = true; leakDetection.Start(); }
/// <summary> /// 超时获取 /// </summary> /// <param name="hardTimeout">毫秒</param> /// <returns></returns> public IDbConnection GetConnection(long hardTimeout) { if (poolState == POOL_SHUTDOWN) { return(null); } if (poolState == POOL_SUSPENDED) { //挂起操作 resetEvent.WaitOne(); } long startTime = DateTime.Now.Ticks; try { long timeout = hardTimeout; do { PoolEntry poolEntry = null; if (connectionBag.TryPop(out poolEntry)) { try { if (poolEntry.State == IConcurrentBagEntry.STATE_REMOVED) { //已经要移除的 CloseConnection(poolEntry.Close()); continue;//继续获取 } keepingExecutor.ScheduleUse(poolEntry); } catch (Exception ex) { throw new Exception("获取失败:" + ex.Message); } //每次产生代理连接,代理连接在外面会关闭,返回连接池的对象 return(poolEntry.CreateProxyConnection(DateTime.Now.Ticks)); } else { CheckPool();//监测连接 if (size < config.MaximumPoolSize) { //创建新的,不再进入集合 poolEntry = CreatePoolEntry(); if (poolEntry != null) { poolEntry.CompareAndSetState(IConcurrentBagEntry.STATE_NOT_IN_USE, IConcurrentBagEntry.STATE_IN_USE); keepingExecutor.ScheduleUse(poolEntry); return(poolEntry.CreateProxyConnection(DateTime.Now.Ticks)); } else { continue; } } } //计算获取的时间,转化成ms timeout = timeout - (DateTime.Now.Ticks - startTime) / tickms; } while (timeout > 0L); } catch (Exception e) { throw new SQLException(poolName + " - Interrupted during connection acquisition", e); } finally { } return(null); }