Пример #1
0
 /// <summary>
 /// 释放申请的数据库连接对象,线程安全
 /// <param name="key">key表示数据库连接申请者</param>
 /// </summary>
 public void DisposeConnection(object key)
 {
     lock (hs_UseConn)
     {
         ConnStruct cs = null;
         if (_ps == PoolState.Run)
         {
             if (!hs_UseConn.ContainsKey(key))
             {
                 throw new NotKeyExecption();//无法释放,不存在的key
             }
             cs          = (ConnStruct)hs_UseConn[key];
             cs.IsRepeat = true;
             if (cs.Allot == false)
             {
                 if (cs.Enable == true)
                 {
                     cs.Allot = true;
                 }
             }
             cs.Remove();
             hs_UseConn.Remove(key);
         }
         else
         {
             throw new PoolNotRunException();//服务未启动
         }
     }
     UpdateAttribute();//更新属性
 }
Пример #2
0
        /// <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;
        }
Пример #3
0
        /// <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数据连接,Access数据库
            }
            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);//MySqlServer数据库连接
            }
            else if (cte == ConnTypeEnum.OracleClient)
            {
                db = new System.Data.OracleClient.OracleConnection(conn);//Oracle数据库连接
            }
            else if (cte == ConnTypeEnum.MsModelClient)
            {
                db = new System.Data.EntityClient.EntityConnection(conn);//Microsoft数据库连接
            }
            ConnStruct cs = new ConnStruct(db, cte, DateTime.Now);

            cs.Open();
            return(cs);
        }
Пример #4
0
 /// <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.Repeat();
         hs_UseConn.Add(key, cs);
         if (cl == ConnLevel.ReadOnly)
         {
             cs.Allot    = false;
             cs.IsRepeat = false;
         }
     }
     catch (Exception e)
     {
         throw new OccasionExecption();//连接资源耗尽,或错误的访问时机。
     }
     finally
     {
         UpdateAttribute();//更新属性
     }
     return(cs.Connection);
 }
Пример #5
0
        /// <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));//返回最合适的连接
        }
Пример #6
0
        /// <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));//返回连接
            }
        }
Пример #7
0
        /// <summary>
        /// 检测事件
        /// </summary>
        private void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ConnStruct cs = null;

            time.Stop();//关闭自己
            isThreadCheckRun = true;
            //如果正在执行创建连接则退出
            if (threadCreate.ThreadState != ThreadState.WaitSleepJoin)
            {
                return;
            }
            //------------------------------------------------------
            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) //没有引用的失效连接
                    {
                        cs.Close();                              //关闭它
                        al_All.Remove(cs);                       //删除
                    }
                }
            }
            //------------------------------------------------------
            UpdateAttribute();                            //更新属性

            if (_spareRealFormPool < _keepRealConnection) //保留空闲实际连接数不足
            {
                createThreadProcessTemp = GetNumOf(_realFormPool, _seepConnection, _maxConnection);
            }
            else
            {
                createThreadProcessTemp = 0;
            }
            //if (createThreadProcessTemp != 0)
            //    Console.WriteLine("创建" + createThreadProcessTemp);

            if (createThreadProcessTemp != 0)
            {
                //启动创建线程,工作模式1
                createThreadMode = 1;
                threadCreate.Interrupt();
            }

            isThreadCheckRun = false;
            time.Start();//打开自己
        }
Пример #8
0
        /// <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;
                }
                return(GetConnectionFormPool_Return(key, cs, ConnLevel.ReadOnly)); //返回得到的连接
            }
            return(GetConnectionFormPool_Return(key, null, ConnLevel.ReadOnly));
        }
Пример #9
0
 /// <summary>
 /// 测试ConnStruct是否过期
 /// </summary>
 /// <param name="cs">被测试的ConnStruct</param>
 private void TestConnStruct(ConnStruct cs)
 {
     //此次被分配出去的连接是否在此次之后失效
     if (cs.UseDegree == _maxRepeatDegree)
     {
         cs.SetConnectionLost();//超过使用次数
     }
     if (cs.CreateTime.AddMinutes(_exist).Ticks <= DateTime.Now.Ticks)
     {
         cs.SetConnectionLost();//连接超时
     }
     if (cs.Connection.State == ConnectionState.Closed)
     {
         cs.SetConnectionLost();//连接被关闭
     }
 }