/// <summary> /// 更新属性 /// </summary> private void UpdateAttribute() { int temp_readOnlyFormPool = 0; //连接池已经分配多少只读连接 int temp_potentRealFormPool = 0; //连接池中存在的实际连接数(有效的实际连接) int temp_spareRealFormPool = 0; //空闲的实际连接 int temp_useRealFormPool = 0; //已分配的实际连接 int temp_spareFormPool = MaxConnectionFormPool; //目前可以提供的连接数 //--------------------------------- lock (hs_UseConn) { _useFormPool = hs_UseConn.Count; } //--------------------------------- ConnStruct cs = null; //int n = 0; lock (al_All) { _realFormPool = al_All.Count; for (int i = 0; i < al_All.Count; i++) { cs = al_All[i]; //只读 if (cs.Allot == false && cs.IsUse == true && cs.IsRepeat == false) { temp_readOnlyFormPool++; } //有效的实际连接 if (cs.Enable == true) { temp_potentRealFormPool++; } //空闲的实际连接 if (cs.Enable == true && cs.IsUse == false) { temp_spareRealFormPool++; } //已分配的实际连接 if (cs.IsUse == true) { temp_useRealFormPool++; } //目前可以提供的连接数 if (cs.Allot == true)//是否可分配 { temp_spareFormPool = temp_spareFormPool - cs.RepeatNow; } else { temp_spareFormPool = temp_spareFormPool - _maxRepeatDegree; } } } _readOnlyFormPool = temp_readOnlyFormPool; _potentRealFormPool = temp_potentRealFormPool; _spareRealFormPool = temp_spareRealFormPool; _useRealFormPool = temp_useRealFormPool; _spareFormPool = temp_spareFormPool; }
/// <summary> /// 用指定类型创建连接 /// </summary> /// <param name="conn">连接字符串</param> /// <param name="cte">连接类型</param> /// <param name="dt">连接超时时间</param> /// <returns>返回创建的连接</returns> private ConnStruct CreateConnection(string conn, ConnTypeEnum cte) { DbConnection db = null; if (cte == ConnTypeEnum.Odbc) { db = new System.Data.Odbc.OdbcConnection(conn);//ODBC数据源连接 } else if (cte == ConnTypeEnum.OleDb) { db = new System.Data.OleDb.OleDbConnection(conn);//OLE DB数据连接 } else if (cte == ConnTypeEnum.SqlClient) { db = new System.Data.SqlClient.SqlConnection(conn);//SqlServer数据库连接 } else if (cte == ConnTypeEnum.MySqlClient) { db = new MySql.Data.MySqlClient.MySqlConnection(conn);//SqlServer数据库连接 } ConnStruct cs = new ConnStruct(db, cte, DateTime.Now); cs.Open(); return(cs); }
/// <summary> /// 释放申请的数据库连接对象,线程安全 /// <param name="RequestKey">key表示数据库连接申请者</param> /// </summary> public void DisposeConnection(object RequestKey) { lock (hs_UseConn) { ConnStruct cs = null; if (_ps == PoolState.Run) { if (!hs_UseConn.ContainsKey(RequestKey)) { throw new NotKeyExecption();//无法释放,不存在的key } cs = (ConnStruct)hs_UseConn[RequestKey]; cs.IsRepeat = true; if (cs.Allot == false) { if (cs.Enable == true) { cs.Allot = true; } } cs.Remove(RequestKey); hs_UseConn.Remove(RequestKey); } else { throw new PoolNotRunException();//服务未启动 } } UpdateAttribute();//更新属性 }
/// <summary> /// 返回DbConnection对象,同时做获得连接时的必要操作 /// </summary> /// <param name="key">key</param> /// <param name="cs">ConnStruct对象</param> /// <param name="cl">级别</param> /// <param name="readOnly">是否为只读属性</param> /// <returns></returns> private DbConnection GetConnectionFormPool_Return(object key, ConnStruct cs, ConnLevel cl) { try { if (cs == null) { //暂无可用连接可分配【移除旧链接重新使用】 throw new Exception(); } cs.LastRunTime = DateTime.Now; cs.Repeat(key); hs_UseConn.Add(key, cs); if (cl == ConnLevel.ReadOnly) { cs.Allot = false; cs.IsRepeat = false; } } catch { throw new OccasionExecption();//连接资源耗尽,或错误的访问时机。 } finally { UpdateAttribute();//更新属性 } return(cs.Connection); }
/// <summary> /// 申请一个连接资源,优先级-低,线程安全 /// </summary> /// <param name="key">申请者</param> /// <returns>申请到的连接对象</returns> protected DbConnection GetConnectionFormPool_Bottom(object key) { ConnStruct cs = null; ConnStruct csTemp = null; for (int i = 0; i < al_All.Count; i++) { csTemp = al_All[i]; if (csTemp.Enable == false || csTemp.Allot == false || csTemp.UseDegree == _maxRepeatDegree)//不可以分配跳出本次循环。 { csTemp = null; continue; } else//不是最合适的放置到最佳选择中 { if (cs != null) { if (csTemp.UseDegree > cs.UseDegree) { //与上一个最佳选择选出一个最佳的放置到cs中 cs = csTemp; } } else { cs = csTemp; } } } return(GetConnectionFormPool_Return(key, cs, ConnLevel.Bottom));//返回最合适的连接 }
/// <summary> /// 申请一个连接资源,优先级-中,线程安全 /// </summary> /// <param name="key">申请者</param> /// <returns>申请到的连接对象</returns> protected DbConnection GetConnectionFormPool_None(object key) { List <ConnStruct> al = new List <ConnStruct>(); ConnStruct cs = null; for (int i = 0; i < al_All.Count; i++) { cs = al_All[i]; if (cs.Enable == false || cs.Allot == false || cs.UseDegree == _maxRepeatDegree)//不可以分配跳出本次循环。 { continue; } if (cs.Allot == true) { al.Add(cs); } } if (al.Count == 0) { return(GetConnectionFormPool_Return(key, null, ConnLevel.None));//发出异常 } else { return(GetConnectionFormPool_Return(key, (al[al.Count / 2]), ConnLevel.None));//返回连接 } }
/// <summary> /// 检查连接池中连接的状态 /// </summary> private void checkThreadProcess() { do { try { ConnStruct cs = null; isThreadCreateRun = true; //如果正在执行创建连接则退出 if (threadCreate.ThreadState != ThreadState.WaitSleepJoin) { continue; } //------------------------------------------------------ lock (al_All) { //int n = 0; for (int i = 0; i < al_All.Count; i++) { cs = al_All[i]; TestConnStruct(cs); //检查连接是否失效 if (cs.Enable == false) //没有引用的失效连接 && cs.RepeatNow == 0 { foreach (var item in cs.RepeatKeyList) { hs_UseConn.Remove(item); } cs.Close(); //关闭它 al_All.Remove(cs); //删除 } } } //------------------------------------------------------ UpdateAttribute();//更新属性 //保留空闲实际连接数不足 if (_spareRealFormPool < _keepRealConnection || _realFormPool < MinConnection) { createThreadProcessTemp = GetNumOf(_realFormPool, _seepConnection, _maxConnection); } else { createThreadProcessTemp = 0; } if (createThreadProcessTemp != 0) { //启动创建线程,工作模式1 createThreadMode = 1; threadCreate.Interrupt(); } isThreadCreateRun = false; } catch { } Thread.Sleep(this.ClearInterval * 1000); } while (this.isThreadCheckRun); }
/// <summary> /// 申请一个连接资源,只读方式,线程安全 /// </summary> /// <param name="key">申请者</param> /// <returns>申请到的连接对象</returns> protected DbConnection GetConnectionFormPool_ReadOnly(object key) { ConnStruct cs = null; for (int i = 0; i < al_All.Count; i++) { cs = al_All[i]; if (cs.Enable == false || cs.Allot == false || cs.UseDegree == _maxRepeatDegree || cs.IsUse == true) { continue; } cs.LastRunTime = DateTime.Now; return(GetConnectionFormPool_Return(key, cs, ConnLevel.ReadOnly)); //返回得到的连接 } return(GetConnectionFormPool_Return(key, null, ConnLevel.ReadOnly)); }
/// <summary> /// 在连接池中申请一个连接,线程安全 /// </summary> /// <param name="key">申请者</param> /// <param name="cl">申请的连接级别</param> /// <returns>返回申请到的连接</returns> public DbConnection GetConnectionFormPool(object key, ConnLevel cl) { lock (this) { if (_ps != PoolState.Run) { throw new StateException();//服务状态错误 } if (hs_UseConn.ContainsKey(key)) { //throw new KeyExecption();//一个key对象只能申请一个连接 ConnStruct cs = (ConnStruct)hs_UseConn[key]; cs.LastRunTime = DateTime.Now; if (cs.State == ConnectionState.Open) { return(cs.Connection); } else { hs_UseConn.Remove(key); return(GetConnectionFormPool_High(key));//High级别 } } if (hs_UseConn.Count == MaxConnectionFormPool) { throw new PoolFullException();//连接池已经饱和,不能提供连接 } if (cl == ConnLevel.ReadOnly) { return(GetConnectionFormPool_ReadOnly(key));//ReadOnly级别 } else if (cl == ConnLevel.High) { return(GetConnectionFormPool_High(key));//High级别 } else if (cl == ConnLevel.None) { return(GetConnectionFormPool_None(key));//None级别 } else { return(GetConnectionFormPool_Bottom(key));//Bottom级别 } } }
/// <summary> /// 测试ConnStruct是否过期 /// </summary> /// <param name="cs">被测试的ConnStruct</param> private void TestConnStruct(ConnStruct cs) { //此次被分配出去的连接是否在此次之后失效 if (cs.UseDegree == _maxRepeatDegree) { cs.SetConnectionLost();//超过使用次数 } if (cs.LastRunTime.AddMinutes(_existMinute).Ticks <= DateTime.Now.Ticks) { cs.SetConnectionLost();//连接超时 } if (cs.Connection.State == ConnectionState.Closed) { cs.SetConnectionLost();//连接被关闭 } if (_spareFormPool == 0 && cs.Equals(al_All.OrderBy(o => o.CreateTime).FirstOrDefault())) { cs.SetConnectionLost();//连接已用完就释放最早的连接 } }
public ItemConnection ContainsConnectionInList(ConnStruct st, List <ItemConnection> list) { foreach (ItemConnection it in list) { if (st.pista1 == it.GetPistaA().item.itemID&& st.pista2 == it.GetPistaB().item.itemID) { if (st.connection == it.connector) { return(it); } } if (st.pista1 == it.GetPistaB().item.itemID&& st.pista2 == it.GetPistaA().item.itemID) { if (st.connection == it.connector) { return(it); } } } return(null); }