示例#1
0
 /// <summary>
 /// 打开链接,允许切从。
 /// </summary>
 internal bool OpenCon(ConnBean cb, AllowConnLevel leve)
 {
     try
     {
         if (cb == null)
         {
             cb = useConnBean;
             if (isOpenTrans && cb.IsSlave)
             {
                 ResetConn(connObject.Master);
             }
         }
         if (!cb.IsOK)
         {
             if ((int)leve > 1 && connObject.BackUp != null && connObject.BackUp.IsOK)
             {
                 ResetConn(connObject.BackUp); //重置链接。
                 connObject.InterChange();     //主从换位置
                 return(OpenCon(connObject.Master, leve));
             }
             else if ((int)leve > 2)
             {
                 //主挂了,备也挂了(因为备会替主)
                 ConnBean nextSlaveBean = connObject.GetSlave();
                 if (nextSlaveBean != null)
                 {
                     ResetConn(nextSlaveBean);//重置链接。
                     return(OpenCon(nextSlaveBean, leve));
                 }
             }
         }
         else if (!isOpenTrans && cb != useConnBean && isAllowResetConn && connObject.IsAllowSlave())
         {
             ResetConn(cb);//,_IsAllowRecordSql只有读数据错误才切,表结构错误不切?
         }
         if (useConnBean.IsOK)
         {
             Open();//异常抛
         }
         else
         {
             WriteError("OpenCon():" + useConnBean.ErrorMsg);
         }
         if (IsAllowRecordSql)
         {
             _watch.Start();
         }
         return(useConnBean.IsOK);
     }
     catch (DbException err)
     {
         useConnBean.IsOK     = false;
         useConnBean.ErrorMsg = err.Message;
         return(OpenCon(null, leve));
     }
 }
示例#2
0
        /// <summary>
        /// 测试链接
        /// </summary>
        /// <param name="allowLevel">1:只允许当前主链接;2:允许主备链接;3:允许主备从链接</param>
        /// <returns></returns>
        internal bool TestConn(AllowConnLevel allowLevel)
        {
            threadCount = 0;
            openOKFlag  = -1;
            ConnObject obj = connObject;

            if (obj.Master != null && obj.Master.IsOK && (int)allowLevel >= 1)
            {
                threadCount++;
                Thread thread = new Thread(new ParameterizedThreadStart(TestOpen));
                thread.Start(obj.Master);
            }
            Thread.Sleep(30);
            if (openOKFlag == -1 && obj.BackUp != null && obj.BackUp.IsOK && (int)allowLevel >= 2)
            {
                threadCount++;
                Thread.Sleep(30);
                Thread thread = new Thread(new ParameterizedThreadStart(TestOpen));
                thread.Start(obj.BackUp);
            }
            if (openOKFlag == -1 && obj.Slave != null && obj.Slave.Count > 0 && (int)allowLevel >= 3)
            {
                for (int i = 0; i < obj.Slave.Count; i++)
                {
                    Thread.Sleep(30);
                    if (openOKFlag == -1 && obj.Slave[i].IsOK)
                    {
                        threadCount++;
                        Thread thread = new Thread(new ParameterizedThreadStart(TestOpen));
                        thread.Start(obj.Slave[i]);
                    }
                }
            }

            int sleepTimes = 0;

            while (openOKFlag == -1)
            {
                sleepTimes += 30;
                if (sleepTimes > 3000 || threadCount == errorCount)
                {
                    break;
                }
                Thread.Sleep(30);
            }
            return(openOKFlag == 1);
        }