Exemple #1
0
        /// <summary>
        /// 处理SqlLog记录
        /// </summary>
        /// <param name="dbi">要记录</param>
        /// <param name="pos"></param>
        /// <param name="translist"></param>
        /// <returns></returns>
        private void DoSqlLog(DbAccessItem dbi, int pos, ArrayList translist)
        {
            try
            {
                if (_sqlLogPool != null)
                {
                    DateTime  dtNow        = HTBaseFunc.GetTime(0);
                    string    occurgroup   = ((pos == 0) ? "" : Environment.TickCount.ToString() + "_" + pos.ToString());
                    ArrayList newtranslist = new ArrayList();

                    ArrayList oParams = new ArrayList();
                    HTDBBaseFunc.StoredProcedureParam lSPparam;
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_poolFlag", "int", _poolFlag, false);
                    oParams.Add(lSPparam);
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recTime", "datetime", dtNow, false);
                    oParams.Add(lSPparam);
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recSql", "text", GenSql(dbi), false);
                    oParams.Add(lSPparam);
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_execResult", "int", ((dbi.Result.ErrCode == 0) ? 0 : 1), false);//执行结果:0-成功,1-错误,2-未执行
                    oParams.Add(lSPparam);
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_errMsg", "text", ((dbi.Result.ErrCode == 0) ? "" : dbi.Result.ErrMsg), false);
                    oParams.Add(lSPparam);
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurGroup", "string", occurgroup, false);
                    oParams.Add(lSPparam);
                    lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurPos", "int", pos, false);
                    oParams.Add(lSPparam);
                    newtranslist.Add(new DbAccessItem("xsp_AddSqlLog", oParams, null, null, true));

                    if (translist != null && translist.Count > 0)
                    {
                        for (int i = pos + 1; i < translist.Count; i++)
                        {
                            DbAccessItem ldbi = (DbAccessItem)translist[i];
                            oParams  = new ArrayList();
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_poolFlag", "int", _poolFlag, false);
                            oParams.Add(lSPparam);
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recTime", "datetime", dtNow, false);
                            oParams.Add(lSPparam);
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recSql", "blob", GenSql(ldbi), false);
                            oParams.Add(lSPparam);
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_execResult", "int", 2, false);
                            oParams.Add(lSPparam);
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_errMsg", "text", "", false);
                            oParams.Add(lSPparam);
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurGroup", "string", occurgroup, false);
                            oParams.Add(lSPparam);
                            lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurPos", "int", i, false);
                            oParams.Add(lSPparam);
                            newtranslist.Add(new DbAccessItem("xsp_AddSqlLog", oParams, null, null, true));
                        }
                    }

                    _sqlLogPool.Push(newtranslist);
                }
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, "DoSqlLog error : {0}.", ex.Message);
            }
        }
Exemple #2
0
 /// <summary>
 /// 数据库访问池将处理结果压回
 /// </summary>
 /// <param name="dai">数据库访问单元</param>
 public void PushCallback(DbAccessItem dai)
 {
     lock (_dbAccessPoolCallbackList)
     {
         _dbAccessPoolCallbackList.Add(dai);
     }
 }
Exemple #3
0
 /// <summary>
 /// 业务逻辑从队列中取出一个处理结果
 /// </summary>
 /// <returns>返回数据库访问单元</returns>
 public DbAccessItem PopupCallback()
 {
     lock (_dbAccessPoolCallbackList)
     {
         if (_dbAccessPoolCallbackList.Count > 0)
         {
             DbAccessItem dai = (DbAccessItem)_dbAccessPoolCallbackList[0];
             _dbAccessPoolCallbackList.RemoveAt(0);
             return(dai);
         }
         else
         {
             return(null);
         }
     }
 }
Exemple #4
0
 /// <summary>
 /// 获取最后几个数据库访问池的运行信息
 /// </summary>
 /// <param name="num">返回的数目</param>
 /// <returns>返回字符串格式的运行信息</returns>
 public string GetLastRunList(int num)
 {
     lock (_dbaccessList)
     {
         string lastrun = "";
         int    pos     = 0;
         for (int i = _lastrunitemlist.Count - 1; i >= 0; i--)
         {
             object item = _lastrunitemlist[i];
             if (item != null)
             {
                 if (item is DbAccessItem)
                 {
                     DbAccessItem dbAccessItem = (DbAccessItem)item;
                     pos++;
                     lastrun += string.Format("【{0}】{1}", pos, dbAccessItem.SpName);
                 }
                 else if (item is ArrayList)
                 {
                     ArrayList translist = (ArrayList)item;
                     string    runsql    = "";
                     for (int j = 0; j < translist.Count; j++)
                     {
                         DbAccessItem dbAccessItem = (DbAccessItem)translist[j];
                         if (runsql != "")
                         {
                             runsql += ";";
                         }
                         runsql += dbAccessItem.SpName;
                     }
                     pos++;
                     lastrun += string.Format("【{0}】{1}", pos, runsql);
                 }
                 //else if (item is PoolAction)
                 //{
                 //    pos++;
                 //    lastrun += string.Format("【{0}】{1}", pos, ((PoolAction)item).Method.Name);
                 //}
             }
             if (pos >= num)
             {
                 break;
             }
         }
         return(lastrun);
     }
 }
Exemple #5
0
        /// <summary>
        /// 获取数据库访问池运行信息
        /// </summary>
        /// <returns>返回字符串格式的运行信息</returns>
        public string GetRunInfo()
        {
            lock (_dbaccessList)
            {
                string runifo = string.Format("RunedNum = {0}, NowTime = {1}, RunTime = {2}, RunItem = ", _runednum, DateTime.Now.ToString("HH:mm:ss"), _runtime.ToString("HH:mm:ss"));

                string runsql = "";
                if (_runitem != null)
                {
                    if (_runitem is DbAccessItem)
                    {
                        DbAccessItem dbAccessItem = (DbAccessItem)_runitem;
                        runsql = dbAccessItem.SpName;
                    }
                    else if (_runitem is ArrayList)
                    {
                        ArrayList translist = (ArrayList)_runitem;
                        for (int i = 0; i < translist.Count; i++)
                        {
                            DbAccessItem dbAccessItem = (DbAccessItem)translist[i];
                            if (runsql != "")
                            {
                                runsql += ";";
                            }
                            runsql += dbAccessItem.SpName;
                        }
                    }
                    //else if (_runitem is PoolAction)
                    //{
                    //    runsql = ((PoolAction)_runitem).Method.Name;
                    //}
                }

                runifo += runsql;
                return(runifo);
            }
        }
Exemple #6
0
        //数据库访问池执行逻辑
        private void Run()
        {
            _threadRunning = true;
            ArrayList logList      = new ArrayList();
            int       lastruntick  = Environment.TickCount;
            int       needDealNum  = 0;
            bool      isGameThread = (_poolFlag & Convert.ToInt32(eDbConnFlag.Game)) != 0x0000 ? true : false;

            while (_threadRunning)
            {
                try
                {
                    sw.Reset();

                    logList.Clear();
                    int    startTick  = Environment.TickCount;
                    object item       = Popup();
                    int    stepTick1  = Environment.TickCount;
                    int    spendTick1 = (startTick == stepTick1) ? 0 : ((startTick < stepTick1) ? stepTick1 - startTick : int.MaxValue - startTick + stepTick1);
                    if (spendTick1 >= 1000)
                    {
                        logList.Add(string.Format("DbAccessPool {0} Popup Delay : {1}.", _poolFlag, spendTick1));
                    }

                    if (item != null)
                    {
                        if (item is DbAccessItem)
                        {
                            int stepTick2 = Environment.TickCount;

                            DbAccessItem dbAccessItem = (DbAccessItem)item;
                            int          rc           = 0;
                            if (dbAccessItem.IsSqlRun)
                            {
                                rc = _htdbf.OpenDataSet(ref dbAccessItem.OutDs, dbAccessItem.SpName, "") ? 0 : -9999;
                            }
                            else
                            {
                                rc = _htdbf.ExecStoredProcedure(dbAccessItem.SpName, ref dbAccessItem.SpParams, ref dbAccessItem.OutDs, "");
                            }
                            if (rc == 0)
                            {
                                dbAccessItem.Result.ErrMsg = "";
                            }
                            else
                            {
                                dbAccessItem.Result.ErrMsg = _htdbf.GetLastError();
                            }
                            dbAccessItem.Result.ErrCode = rc;

                            int stepTick3  = Environment.TickCount;
                            int spendTick3 = (stepTick2 == stepTick3) ? 0 : ((stepTick2 < stepTick3) ? stepTick3 - stepTick2 : int.MaxValue - stepTick2 + stepTick3);
                            if (spendTick3 >= 1000)
                            {
                                logList.Add(string.Format("DbAccessPool {0} Run1 Delay : {1}, {2}.", _poolFlag, spendTick3, dbAccessItem.SpName));
                            }

                            if (rc != 0 &&
                                (_poolFlag & Convert.ToInt32(eDbConnFlag.SqlLog)) == 0x0000 &&
                                dbAccessItem.Result.ErrMsg != "" &&
                                dbAccessItem.Result.ErrMsg != _htdbf.GetErrPerfix())
                            {
                                DoSqlLog(dbAccessItem, 0, null);
                            }

                            if (dbAccessItem.OnFinish != null)
                            {
                                int stepTick4 = Environment.TickCount;
                                if (_vistor != null)
                                {
                                    _vistor.PushCallback(dbAccessItem);
                                }
                                int stepTick5  = Environment.TickCount;
                                int spendTick5 = (stepTick4 == stepTick5) ? 0 : ((stepTick4 < stepTick5) ? stepTick5 - stepTick4 : int.MaxValue - stepTick4 + stepTick5);
                                if (spendTick5 >= 1000)
                                {
                                    logList.Add(string.Format("DbAccessPool {0} Run1PushBack Delay : {1}.", _poolFlag, spendTick5));
                                }
                            }
                        }
                        else if (item is ArrayList)
                        {
                            int    stepTick2 = Environment.TickCount;
                            string logStr    = "";

                            ArrayList    translist    = (ArrayList)item;
                            DbAccessItem dbAccessItem = null;
                            int          errpos       = -1;
                            for (int i = 0; i < translist.Count; i++)
                            {
                                int stepTick3 = Environment.TickCount;

                                dbAccessItem = (DbAccessItem)translist[i];
                                _htdbf.HoldDbTransNoCommit = true;

                                int rc = 0;
                                if (dbAccessItem.IsSqlRun)
                                {
                                    rc = _htdbf.OpenDataSet(ref dbAccessItem.OutDs, dbAccessItem.SpName, "") ? 0 : -9999;
                                }
                                else
                                {
                                    rc = _htdbf.ExecStoredProcedure(dbAccessItem.SpName, ref dbAccessItem.SpParams, ref dbAccessItem.OutDs, "");
                                }
                                logStr += string.Format("{1}({0})", i + 1, dbAccessItem.SpName);

                                int stepTick4  = Environment.TickCount;
                                int spendTick4 = (stepTick3 == stepTick4) ? 0 : ((stepTick3 < stepTick4) ? stepTick4 - stepTick3 : int.MaxValue - stepTick3 + stepTick4);
                                if (spendTick4 >= 1000)
                                {
                                    logList.Add(string.Format("DbAccessPool {0} Run2Sub Delay : {1}, {2}.", _poolFlag, spendTick4, dbAccessItem.SpName));
                                }

                                if (rc != 0)
                                {
                                    errpos = i;
                                    dbAccessItem.Result.ErrCode = rc;
                                    dbAccessItem.Result.ErrMsg  = _htdbf.GetLastError();

                                    if ((_poolFlag & Convert.ToInt32(eDbConnFlag.SqlLog)) == 0x0000 && dbAccessItem.Result.ErrMsg != "" && dbAccessItem.Result.ErrMsg != _htdbf.GetErrPerfix())
                                    {
                                        DoSqlLog(dbAccessItem, errpos, translist);
                                    }

                                    break;
                                }
                            }

                            int stepTick5  = Environment.TickCount;
                            int spendTick5 = (stepTick2 == stepTick5) ? 0 : ((stepTick2 < stepTick5) ? stepTick5 - stepTick2 : int.MaxValue - stepTick2 + stepTick5);
                            if (spendTick5 >= 1000)
                            {
                                logList.Add(string.Format("DbAccessPool {0} Run2 Delay : {1}, {2}.", _poolFlag, spendTick5, logStr));
                            }

                            if (dbAccessItem != null)
                            {
                                int stepTick6 = Environment.TickCount;
                                if (dbAccessItem.Result.ErrCode == 0)
                                {
                                    _htdbf.CommitTransaction("");
                                }
                                else
                                {
                                    _htdbf.RollbackTransaction();
                                }
                                int stepTick7  = Environment.TickCount;
                                int spendTick7 = (stepTick6 == stepTick7) ? 0 : ((stepTick6 < stepTick7) ? stepTick7 - stepTick6 : int.MaxValue - stepTick6 + stepTick7);
                                if (spendTick7 >= 1000)
                                {
                                    logList.Add(string.Format("DbAccessPool {0} Run2Tran Delay : {1}, {2}.", _poolFlag, spendTick7, dbAccessItem.Result.ErrCode));
                                }

                                if (dbAccessItem.OnFinish != null)
                                {
                                    int stepTick8 = Environment.TickCount;
                                    if (_vistor != null)
                                    {
                                        _vistor.PushCallback(dbAccessItem);
                                    }
                                    int stepTick9  = Environment.TickCount;
                                    int spendTick9 = (stepTick8 == stepTick9) ? 0 : ((stepTick8 < stepTick9) ? stepTick9 - stepTick8 : int.MaxValue - stepTick8 + stepTick9);
                                    if (spendTick9 >= 1000)
                                    {
                                        logList.Add(string.Format("DbAccessPool {0} Run2PushBack Delay : {1}.", _poolFlag, spendTick9));
                                    }
                                }
                            }
                        }

                        //每次有数据库操作就更新下上次执行时间
                        lastruntick = Environment.TickCount;
                    }

                    OneRunOver((item != null ? 1 : 0));

                    if (logList.Count > 0 && _vistor != null)
                    {
                        _vistor.PushLog(_poolFlag, logList);
                    }

                    sw.Stop();
                    needDealNum = WaitedDbAccessNum();
                    if (isGameThread == true && needDealNum > 0)
                    {
                        ++_doWorkTimes;
                    }
                    if (needDealNum == 0 || _doWorkTimes > 100)
                    {
                        if (sw.Elapsed < _maxSleepTS)
                        {
                            Thread.Sleep(_maxSleepTS - sw.Elapsed);
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }

                        _doWorkTimes = 0;
                    }
                }
                catch (ThreadAbortException)
                {
                }
                catch (Exception ex)
                {
                    if (_logger != null)
                    {
                        _logger.Fatal(ex, ex.Message);
                    }
                }
            }
        }
Exemple #7
0
        private string GenSql(DbAccessItem dbi)
        {
            if (dbi.IsSqlRun)
            {
                return(dbi.SpName);
            }
            else
            {
                string sql = "", outsql = "";
                try
                {
                    HTDBBaseFunc.ConnectionTypeEnum dbtype = GetConnectType(_dbType);
                    if (dbtype == HTDBBaseFunc.ConnectionTypeEnum.MySql || dbtype == HTDBBaseFunc.ConnectionTypeEnum.MySqlUseTran)
                    {
                        for (int i = 0; i < dbi.SpParams.Count; i++)
                        {
                            if (sql != "")
                            {
                                sql += ", ";
                            }

                            HTDBBaseFunc.StoredProcedureParam sparm = (HTDBBaseFunc.StoredProcedureParam)dbi.SpParams[i];
                            if (sparm.Type.Trim().ToLower() == "image" || sparm.Type.Trim().ToLower() == "blob")
                            {
                                if (outsql != "")
                                {
                                    outsql += "\n";
                                }
                                string pname = "@p" + i.ToString();
                                outsql += "set " + pname + "=" + HTBaseFunc.GetSqledValue(sparm.Value.GetType(), sparm.Value);
                                sql    += pname;
                            }
                            else
                            {
                                if (sparm.Direction == ParameterDirection.Output || sparm.Direction == ParameterDirection.InputOutput)
                                {
                                    if (outsql != "")
                                    {
                                        outsql += "\n";
                                    }
                                    string pname = "@p" + i.ToString();
                                    outsql += "set " + pname + "=" + HTBaseFunc.GetSqledValue(sparm.Value.GetType(), sparm.Value);
                                    if (sparm.Direction == ParameterDirection.Output)
                                    {
                                        sql += "out " + pname;
                                    }
                                    else
                                    {
                                        sql += "inout " + pname;
                                    }
                                }
                                else
                                {
                                    sql += HTBaseFunc.GetSqledValue(sparm.Value.GetType(), sparm.Value);
                                }
                            }
                        }
                        sql = "call " + dbi.SpName + "(" + sql + ");";
                    }
                    else if (dbtype == HTDBBaseFunc.ConnectionTypeEnum.Sql || dbtype == HTDBBaseFunc.ConnectionTypeEnum.SqlOleDB || dbtype == HTDBBaseFunc.ConnectionTypeEnum.SybaseOleDB ||
                             dbtype == HTDBBaseFunc.ConnectionTypeEnum.Oracle)
                    {
                        for (int i = 0; i < dbi.SpParams.Count; i++)
                        {
                            if (sql != "")
                            {
                                sql += ", ";
                            }

                            HTDBBaseFunc.StoredProcedureParam sparm = (HTDBBaseFunc.StoredProcedureParam)dbi.SpParams[i];
                            sql += sparm.Name + "=" + HTBaseFunc.GetSqledValue(sparm.Value.GetType(), sparm.Value);
                            if (sparm.Direction == ParameterDirection.Output || sparm.Direction == ParameterDirection.InputOutput)
                            {
                                sql += " output";
                            }
                        }
                        sql = "exec " + dbi.SpName + " " + sql + ";";
                    }
                    if (outsql != "")
                    {
                        sql = outsql + "\n" + sql;
                    }
                }
                catch (Exception ex)
                {
                    sql = dbi.SpName + string.Format("(Param generate error : {0})", ex.Message);
                }
                return(sql);
            }
        }