当前位置: 代码迷 >> C# >> 连接池类。现在要加一个如其网断了就1分钟检测一下,能连上就继续工作
  详细解决方案

连接池类。现在要加一个如其网断了就1分钟检测一下,能连上就继续工作

热度:185   发布时间:2016-04-28 08:35:13.0
连接池类。现在要加一个如果网断了就1分钟检测一下,能连上就继续工作
第一次接触连接池。
经理说前段时间网断了,让优化一下连接池
如果网断了就1分钟检测一下
连上了就继续工作

下面上代码
因为不能复制,有字母错误请略过


public class SqlConnPool
{
    private staric SqlConnectionPool _SqlPool=new SqlConnectionPool();
    public static SqlConnectionPool SqlPool
    {
        get{ return _SqlPool; }
    }

    public class SingleSqlConn
    {
        public SqlConnection sqlconn;
        public SqlCommand slqcmd;

        public SingleSqlConn(string connstr)
        try
        {
            sqlconn.Open();
            sqlcmd=sqlconn.CreateCommand();    
        }
        catch{}    
    }

    public class SqlConnectionPool
    {
        public int size=10;  //最大连接数
        private Queue<SingleSqlConn> pool;  //连接池
        private static readonly object  _object=new object(); //锁对象
        private string _sConnStr=stringl.Empty;//连接字符串
        private const int Overflow=500;
        private int _iOverflow=500;//超时次数
        private const int _iWaitMillsecondes=1000; //为获得连接线程等待时间
        private Mutex mutex;

        public int QueueCnt
        {
            get
            {
                lock(_object)
                {
                    return pool.Count;
                }
            }
        }

        public SqlConnectionPool()
        {
            pool=new Queue<SingleSqlConn>(1000);
            mutex=new Mutex();
        }

        public int open(string connstr,int maxsize=10) //初始化连接池
        {
            int iRet=0;
            _sConnStr=connstr;
            size=maxsize;

            while(iRet<size)
            {
                SingleSqlConn tmp=new SingleSqlConn(_sConnStr);
                if(tmp.sqlconn.State!=ConnectionState.Open)
                {
                    Thread.Sleep(15);
                    continue;
                }
                tmp.sqlcmd.CommandText="select 1";
                if(tmp!=null)
                {
                    lock(_object)
                    {
                        pool.Enqueue(tmp);
                    }
                    iRet++;
                }
            }
            return iRet;
        }

        public SingleSqlConn getConnection()
        {
            SingleSqlConn tmp=null;
            if(QueueCnt>0)
            {
                lock(_object)
                {
                    tmp=pool.Dequeue();
                }
                if(tmp==null)
                {
                    tmp=ssNewConnection(); //获得的链接是null,生成新连接
                }
                else
                {
                    _iOverflow=Overflow;
                }
            }
            else
            {
                Thread.Sleep(_iWaitMilisecondes);
                if(--_ioverflow<0)
                    return null;
                else
                {
                    tmp=getConnection();//重试获得连接
                }
            }
            return tmp;
        }

        public void returnConnection(SingleSqlConn con)
        {
            try
            {
                con.sqlcmd.CommandText="select 1";
            }
            catch
            {
                vCloseConnection(con);

                lock(_object)
                {
                    pool.Enqueue(ssNewConnection());
                }
                return ;
            }
            if(QueueCnt<=size)
            {
                lock(_object)
                {
                    pool.Enqueue(con);//归还一个连接
                }
            }
            else
            {
                vCliseConnection(con);//销毁一个连接
            }
        }

        private SingleSqlConn ssNewConnection()
        {
            SingleSqlConn tmp=new SingleSqlConn(_sConnStr);
            if(tmp.sqlconn.State==ConnectionState.Closed)
            {
                tmp=ssNewConnection();获得一个空连接
            }
            tmp.sqlcmd.CommandText="select 1";
            return tmp;
        }

        private void vCloseConnection(SingleSqlConn ssc)
        {
            ssc.sqlcmd.Dispose();
            ssc.sqlconn.Dispose();
            ssc.sqlconn.Close();
        }
    }
}

------解决思路----------------------
捕获连接错误,做网络连接测试,测试成功再次启动程序
  相关解决方案