C#Sqltimeout命令不知道程序端还是sql服务器端

时间:2019-02-18 09:15:49

标签: c# sql-server timeout timeoutexception

我有一个C#程序,可连接到sql server并获取数据。 连接到sql的程序的这一部分有很多类和继承不是我写的, 我从未设法从中获取sqltimeout异常,该异常连接到sql服务器并接收数据。

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

这是错误:

  

System.Exception:序列1中的进程'dbsource'命令出错:   运行“ dbsource”命令时出错:System.Data.SqlClient.SqlException   (0x80131904):超时已过期。逾时时间已过   操作完成或服务器没有响应。 ->   System.ComponentModel.Win32Exception(0x80004005):等待操作   时间到       在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,动作1 wrapCloseInAction)       在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject   stateObj,布尔值调用者HasConnectionLock,布尔值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);
                }
            }
        }

0 个答案:

没有答案