Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated

后端 未结 20 2184
臣服心动
臣服心动 2020-11-22 10:23

I have many users on my web site (20000-60000 per day), which is a download site for mobile files. I have remote access to my server (windows server 2008-R2).
I\'ve

相关标签:
20条回答
  • 2020-11-22 10:57

    Looks like you have a query that is taking longer than it should. From your stack trace and your code you should be able to determine exactly what query that is.

    This type of timeout can have three causes;

    1. There's a deadlock somewhere
    2. The database's statistics and/or query plan cache are incorrect
    3. The query is too complex and needs to be tuned

    A deadlock can be difficult to fix, but it's easy to determine whether that is the case. Connect to your database with Sql Server Management Studio. In the left pane right-click on the server node and select Activity Monitor. Take a look at the running processes. Normally most will be idle or running. When the problem occurs you can identify any blocked process by the process state. If you right-click on the process and select details it'll show you the last query executed by the process.

    The second issue will cause the database to use a sub-optimal query plan. It can be resolved by clearing the statistics:

    exec sp_updatestats
    

    If that doesn't work you could also try

    dbcc freeproccache
    

    You should not do this when your server is under heavy load because it will temporarily incur a big performace hit as all stored procs and queries are recompiled when first executed. However, since you state the issue occurs sometimes, and the stack trace indicates your application is starting up, I think you're running a query that is only run on occasionally. You may be better off by forcing SQL Server not to reuse a previous query plan. See this answer for details on how to do that.

    I've already touched on the third issue, but you can easily determine whether the query needs tuning by executing the query manually, for example using Sql Server Management Studio. If the query takes too long to complete, even after resetting the statistics you'll probably need to tune it. For help with that, you should post the exact query in a new question.

    0 讨论(0)
  • 2020-11-22 10:57

    While all the earlier responses address the issue they did not cover all cases.

    Microsoft has acknowledged the issue and fixed it in 2011 for supported operating systems, so if you get the stack trace like:

    Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
    at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
    at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
    

    you may need to update your .NET assemblies.

    This issue occurs because of an error in the connection-retry algorithm for mirrored databases.

    When the retry-algorithm is used, the data provider waits for the first read (SniReadSync) call to finish. The call is sent to the back-end computer that is running SQL Server, and the waiting time is calculated by multiplying the connection time-out value by 0.08. However, the data provider incorrectly sets a connection to a doomed state if a response is slow and if the first SniReadSync call is not completed before the waiting time expires.

    See KB 2605597 for details

    https://support.microsoft.com/kb/2605597

    0 讨论(0)
  • 2020-11-22 10:57

    We recently upgraded to the NuGet version of SqlClient (Microsoft.Data.SqlClient) which contains a bug. This bug was introduced during the lifetime of the 1.x cycle and has already been fixed. The fix will be available in the 2.0.0 release which is not available at the time of this writing. A preview is available.

    You can inspect the details here: https://github.com/dotnet/SqlClient/issues/262

    0 讨论(0)
  • 2020-11-22 10:59

    Maybe it will be useful for somebody. I faced with the same problem and in my case the reason was the SqlConnection was opened and not disposed in the method that I called in loop with about 2500 iterations. Connection pool was exhausted. Proper disposing solved the problem.

    0 讨论(0)
  • 2020-11-22 11:02

    Default timeout is 15 seconds, to change that, 0 is unlimited, any other number is the number of seconds.

    In Code

    using (SqlCommand sqlCmd = new SqlCommand(sqlQueryString, sqlConnection))
       {
          sqlCmd.CommandTimeout = 0; // 0 = give it as much time as it needs to complete
          ...
        }
    

    In Your Web.Config, "Command Timeout=0;" do not time out, or as below 1 hour (3600 seconds)

      <add name="ConnectionString" connectionString="Data Source=ServerName;User ID=UserName;Password=Password;Command Timeout=3600;" providerName="System.Data.SqlClient" />
    
    0 讨论(0)
  • 2020-11-22 11:02

    If you are using ASP.NET Core with the Startup.cs convention, you can access and set the query command timeout option like this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<MyDbContext>(_ =>
        {
            _.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"), options => 
            {
                options.CommandTimeout(180); // 3 minutes
            });
        });
    }
    
    0 讨论(0)
提交回复
热议问题