English 中文(简体)
为什么(某个特定服务器)ADO,NET会获得时间 db结果退还后的例外情形,读者正在关闭?
原标题:Why (on a specific server) would ADO,NET get a Time Out exception after db result is returned and the reader is being closed?

我在伙伴关系中有一些奇怪的行为。 我正在尝试确定的网络应用。

我正在从法典中找到以下错误:

停机。 运行完成之前或服务器尚未答复的截止日期。

我熟悉这一错误及其许多原因。 我所有典型的射击渠道都失败了。

以下是一些动态:

服务器最近重建。 因此,这可以成为服务器配置问题。

这一错误只出现在特定的网络服务器上。 当我在当地管理该应用程序时,从其他服务器开始,该应用程序很快。 我无法从我的机器中再次向MS中的赞成者提出问题。

  • This tells me that it is specific to this server.

一. 导言 采用SOSql指挥线的同一建议 它行之有效。 快速。

  • This indicates that it is likely something .NET related, NOT db related

在实施该守则之前,我已在该服务器和同一行实施了其他储存程序。

  • This suggests the proc may be related , but is not a "server X cannot talk to server Y"

我从非行所有者那里得到确认,即非行已收到指挥、执行(>1second )并退还数据。

  • By tracing the code, I see the results are returned and it is not until I try to close the data reader that the error occurs.
  • In fact I see it takes 36 milliseconds to execute the stored procedure and iterate through the result.

它喜欢要求数据检索。 时间已经接近,最终时间已经结束。

我已将最高人才库从31个增加到100个。

Here is a sample of what my code looks like, how it is structured. I have been hacking at it for trouble shooting: I have added the explict close to ensure I know where the error occurs. There may be syntax issues: I have made it generic, and may have introduced bugs in doing so.

    public double  GetMyData()
    {
        double returnValue;
       // Used in logging to see if code was reached & how long it took.
      System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); 
            using (SqlConnection cn = Connections.GetSqlConnection())
        {
            cn.Open();
            using (SqlCommand cmd = getSQLCommmand("SomeProcName"))
            {

                Log.Log.WriteTrace(string.Format("Execute {0} ","SomeProcName"),0);
                s.Start();
                SqlDataReader dr= null;
                try
                {
                     dr = cmd.ExecuteReader();
                     s.Stop();
                     Log.Log.WriteTrace("Timer", "ExecuteReader done " + s.ElapsedMilliseconds + "ms ", 0);
                     s.Start();


                    if (dr != null)
                    {
                        if (dr.Read())
                        {
                            returnValue =
                                Conversion.DBNullToDouble(
                                    dr[0]);
                        }
                        s.Stop();
                        Log.Log.WriteTrace("Timer", "dr.read done  (result:" + returnValue + ")" + s.ElapsedMilliseconds + "ms ", 0); // I get Here
                    }
                }catch(Exception ex)
                {
                    Log.Log.PersistException(ex);
                }
                //}
                if(dr!=null && !dr.IsClosed)
                    dr.Close();// This times out
                if (cn != null && cn.State !=ConnectionState.Closed)
                        cn.Close();
                Log.Log.WriteTrace("DONE "),
                                 ;
            }
        }
            return (returnValue);
    }

<>>>>>

<代码>dr.Close(>); 限时2分钟。 就在这个服务器上。 从当地来看,这只字不提。

<>>>>>

1. 接受的答复: 我有一份有多个记录的文件。 我正在 taking。 拨号为Cancel()并不固定,但大大缩短了关闭数据阅读器所需的时间。 探索这一点应当有助于我解决问题。 我不知道为什么这只是在这个服务机上发生的,因为服务机是一家发展中国家服务机。

最佳回答

我在你的法典中看到几个问题。

  1. Try to use the Reader in a using statement it closes and disposes when finishing it.
  2. 您是联系的两倍(在使用时)。

    if (cn != null && cn.State !=ConnectionState.Closed) cn.Close(); and in the using (SqlConnection cn = Connections.GetSqlConnection()) -- this one do it for you at the end of the statement

  3. 在你的法典中,你没有检查。 数据检索。 HasRows 因此,如果您的Sproc没有回馈价值,那么它将在if(dr.Read()上作出例外规定,从而可能成为你为什么有时会取消例外的原因。

  4. If you only want to retrieve the value of the first column of the first row you should take a look to ExecuteScalar

Finally I would rewrite your code like this (using DataReader)

public double  GetMyData()
    {
        double returnValue;
       // Used in logging to see if code was reached & how long it took.
      System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); 
            using (SqlConnection cn = Connections.GetSqlConnection())
        {
            cn.Open();
            using (SqlCommand cmd = getSQLCommmand("SomeProcName"))
            {

                Log.Log.WriteTrace(string.Format("Execute {0} ","SomeProcName"),0);
                s.Start();
                using(SqlDataReader dr = cmd.ExecuteReader())
                {


                     s.Stop();
                     Log.Log.WriteTrace("Timer", "ExecuteReader done " + s.ElapsedMilliseconds + "ms ", 0);
                     s.Start();


                    if (dr != null)
                    {
                        if(dr.HasRows)
                        {
                           if (dr.Read())
                           {
                              returnValue =
                                Conversion.DBNullToDouble(
                                    dr[0]);
                           }
                        }
                        s.Stop();
                        Log.Log.WriteTrace("Timer", "dr.read done  (result:" + returnValue + ")" + s.ElapsedMilliseconds + "ms ", 0); // I get Here
                    }
                }             
            }
        }
            return (returnValue);
    }

或(外币)

public double  GetMyData()
        {
            double returnValue;
           // Used in logging to see if code was reached & how long it took.
          System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); 
                using (SqlConnection cn = Connections.GetSqlConnection())
            {
                cn.Open();
                using (SqlCommand cmd = getSQLCommmand("SomeProcName"))
                {

                    Log.Log.WriteTrace(string.Format("Execute {0} ","SomeProcName"),0);
                    s.Start();
                    try
                    {
                         returnValue = Conversion.DBNullToDouble(cmd.ExecuteScalar());
                         s.Stop();  
                    }   
                    catch(Exception ex)
                    {
                        Log.Log.PersistException(ex);
                    }

                }
            }
                return (returnValue);
        }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!