コード例 #1
0
ファイル: DBManager.cs プロジェクト: FRee22858/ServerCluster
        public void Run()
        {
            var tempPostUpdateQueue = new Queue <AbstractDBQuery>();
            var time = new SystemTime();

            time.Init();
            while (true)
            {
                var dt = time.Update();
                _lasttime = dt.TotalMilliseconds;
                if (_lasttime > 1)
                {
                    Thread.Sleep(0);
                }
                else
                {
                    Thread.Sleep(1);
                }
                if (_totaltime > 10000)
                {
                    _totaltime = 0;
                }
                else
                {
                    _totaltime += _lasttime;
                }
                lock (m_cReconnectInfo)
                {
                    if (IsDisconnected() || m_cReconnectInfo.NeedReconnect)
                    {
                        m_bOpened = false;
                        string log = string.Format("disconnect from db{0},eslplased {1} ms", _strDatabase, m_cReconnectInfo.TryConnectTime);
                        AddExceptionLog(log);
                        string strConn = string.Format("data source={0}; database={1}; user id={2}; password={3}; port={4}", _strIp, _strDatabase, _strUsername, _strPassword, _strPort);
                        try
                        {
                            _conn = new MySqlConnection(strConn);
                            _conn.Open();
                            m_bOpened = true;
                            m_cReconnectInfo.Reset();
                        }
                        catch (MySqlException e)
                        {
                            LOG.Error(e.ToString());
                            AddExceptionLog(e.ToString());
                        }
                    }
                    else
                    {
                        try
                        {
                            lock (_saveQueue)
                            {
                                if (_saveQueue.Count == 0)
                                {
                                    continue;
                                }
                                while (_saveQueue.Count > 0)
                                {
                                    AbstractDBQuery query = _saveQueue.Dequeue();
                                    _executionQueue.Enqueue(query);
                                }
                            }
                            while (_exceptionLogQueue.Count != 0)
                            {
                                var  query   = _executionQueue.Dequeue();
                                bool success = query.Execute();
                                if (success == false)
                                {
                                    if (query.m_strErrorText != null)
                                    {
                                        AddExceptionLog(query.m_strErrorText);
                                    }
                                }
                                else
                                {
                                }
                                tempPostUpdateQueue.Enqueue(query);
                            }
                            lock (_postUpdateQueue)
                            {
                                while (tempPostUpdateQueue.Count > 0)
                                {
                                    _postUpdateQueue.Enqueue(tempPostUpdateQueue.Dequeue());
                                }
                            }
                            tempPostUpdateQueue.Clear();
                        }
                        catch (Exception e)
                        {
                            LOG.Error(e.ToString());
                        }
                    }
                }
            }
        }