首页 > 解决方案 > C# Sqltimeout 命令不知道程序端还是 sql server 端

问题描述

我有连接到 sql server 并获取数据的 ac# 程序。连接到 sql 的这部分程序有很多不是我编写的类和继承,我从来没有设法从中获取 sqltimeout 异常,它连接到 sql server 并接收数据。

但是我必须重新启动我的程序并且 sql 超时消失了。我不知道是 sql 部分锁定了存储过程还是我的程序锁定?

这是错误:

System.Exception:在序列 1 中处理“dbsource”命令时出错:运行“dbsource”命令时出错:System.Data.SqlClient.SqlException (0x80131904):超时已过期。在操作完成之前超时时间已过或服务器没有响应。---> System.ComponentModel.Win32Exception (0x80004005): System.Data.SqlClient.SqlConnection.OnError(SqlException 异常, Boolean breakConnection, Action`1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning 的等待操作超时(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)

这是名为 dbsource 的 sqlconnection 类的一部分:

using (var con = new SqlConnection(setting.ConnectionString))
                    {
                        using (SqlCommand command = new SqlCommand("SET ARITHABORT ON", con))
                        {
                            try
                            {
                                command.CommandText = base.HtmlCommand.Attributes["procedurename"];
                            }
                            catch
                            {
                                if (string.IsNullOrEmpty(setting.ProcedureName))
                                {
                                    command.CommandText = HtmlCommand.HttpContext.CurrentSetting.DEFAULT_ProcedureName;
                                }
                                else
                                {
                                    command.CommandText = setting.ProcedureName;
                                }
                            }

                            var paramsNode = HtmlCommand.TagElements.SingleOrDefault(x => x.TagName == "params");
                            var nameValuePairTblParam = new System.Data.DataTable();
                            nameValuePairTblParam.Columns.Add("name");
                            nameValuePairTblParam.Columns.Add("Value");
                            if (paramsNode != null)
                            {
                                foreach (var node in paramsNode.TagElements)
                                {
                                    try
                                    {
                                        nameValuePairTblParam.Rows.Add(node.GetAttributeValue("name"), node.GetAttributeValue("value", true));
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("Error in Read Params Section!", ex);
                                    }
                                }
                            }
                            command.CommandType = CommandType.StoredProcedure;
                            StringBuilder sb = new StringBuilder();
                            HtmlCommand.WriteOuterHtml(sb, "params");
                            command.Parameters.Add(new SqlParameter("@c", sb.ToString()));
                            command.Parameters.Add(new SqlParameter("@x", HtmlCommand.HttpContext.Request.DomainId));
                            command.Parameters.Add(new SqlParameter("@y", nameValuePairTblParam));

                            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                            {                                
                                    adapter.Fill(dataSet);                                                               
                            }
                        }
                    }

有一个 RenderingWrapper 方法以并行方式管理 sqlConnctions:

private void RenderingWrapper(bool async)
        {
WaitToLoadAllPendingSource(Core, true);
}

protected void WaitToLoadAllPendingSource(string core, bool forIfAttrOnly)
        {
            CheckDataSourcePending(forIfAttrOnly);
            DataSourceAdded();
            if (PendingSource.Count != 0)
            {
                if (OwnerGroup.AllSourceLoaded)
                {
#if DEBUG
                    Util.Log(string.Format("{0:000#} - {1} Stop Process For Not Exist Datasource '{2}' Without Wait...", ID, core, string.Join("','", PendingSource)), ConsoleColor.Red);
#endif
                    return;
                }
#if DEBUG
                Util.Log(string.Format("{0:000#} - {1} Wait Start For '{2}'...", ID, core, string.Join("','", PendingSource)), ConsoleColor.DarkCyan);
#endif
                _waitHandle = new AutoResetEvent(false);
                Thread process = new Thread(() =>
                {
                    while (_waitHandle != null)
                    {
                        Thread.Sleep(1);
                        CheckDataSourcePending(forIfAttrOnly);
                        DataSourceAdded();
                    }
                });

                process.Start();
                _waitHandle.WaitOne(Parent.HttpContext.CurrentSetting.DEFAULT_ExecuteCMSCommandWaitTimeout);
                _waitHandle.Dispose();
                _waitHandle = null;
#if DEBUG
                Util.Log(string.Format("{0:000#} - {1} Wait End.", ID, core), ConsoleColor.DarkCyan);
#endif
                CheckDataSourcePending(forIfAttrOnly);
                DataSourceAdded();
            }
        }

public virtual void DataSourceAdded()
        {
            lock (_check_data_source_lock)
            {

                if (PendingSource != null)
                {
                    foreach (var pendingSource in PendingSource.ToArray())
                    {
                        try
                        {
                            OwnerGroup.GetSource(pendingSource);
                            //check if null
                            //if added by reza
                            if (PendingSource != null)
                                PendingSource.Remove(pendingSource);
                        }
                        catch {/*Nothing*/}
                    }
                }

                if (PendingSource == null || PendingSource.Count == 0)
                {
                    try
                    {
                        if (_waitHandle != null)
                        {
                            _waitHandle.Set();
                        }
                    }
                    catch { /*Nothing*/}
                }
            }
        }


public override void CheckDataSourcePending(bool forIfAttrOnly)
        {
            if (Attributes != null)
            {
                if (forIfAttrOnly)
                {
                    try
                    {
                        var value = Attributes["filter"];
                        Attributes["filter"] = InitalizeStringFromSource(value);
                    }
                    catch {/*Nothing*/ }
                }
                else
                {
                    foreach (var key in Attributes.Keys.ToArray())
                    {
                        try
                        {
                            var value = Attributes[key];
                            Attributes[key] = InitalizeStringFromSource(value);
                        }
                        catch { /*Nothing*/}
                    }
                }
            }
            if (Elements != null && !forIfAttrOnly)
            {
                foreach (HtmlElement child in Elements)
                {
                    child.CheckDataSourcePending(false);
                }
            }
        }

标签: c#sql-servertimeouttimeoutexception

解决方案


推荐阅读