title: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated
body: <p>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). <br/>  I've received <em>&quot;Server is unavailable&quot;</em> errors before, but I am now seeing a connection timeout error.
  <br/>  I'm not familiar with this - why does it occur and how can I fix it?</p>
  <p>The full error is below:</p>
  <blockquote>
  <p>Server Error in '/' Application. Timeout expired.  The timeout period
  elapsed prior to completion of the operation or the server is not
  responding. The statement has been terminated. Description: An
  unhandled exception occurred during the execution of the current web
  request. Please review the stack trace for more information about the
  error and where it originated in the code.</p>
  <p>Exception Details: System.Data.SqlClient.SqlException: Timeout
  expired.  The timeout period elapsed prior to completion of the
  operation or the server is not responding. The statement has been
  terminated.</p>
  <p>Source Error:</p>
  <p>An unhandled exception was generated during the execution of the
  current web request. Information regarding the origin and location of
  the exception can be identified using the exception stack trace below.</p>
  <p>Stack Trace:</p>
  <p>[SqlException (0x80131904): Timeout expired.  The timeout period
  elapsed prior to completion of the operation or the server is not
  responding. The statement has been terminated.]<br />
  System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
  Boolean breakConnection) +404<br />
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +412<br />
  System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
  SqlCommand cmdHandler, SqlDataReader dataStream,
  BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
  stateObj) +1363<br />
  System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,
  RunBehavior runBehavior, String resetOptionsString) +6387741<br />
  System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
  cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean
  async) +6389442<br />
  System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
  cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
  method, DbAsyncResult result) +538<br />
  System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
  result, String methodName, Boolean sendToPipe) +689<br />
  System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +327<br />
  NovinMedia.Data.DbObject.RunProcedure(String storedProcName,
  IDataParameter[] parameters, Int32&amp; rowsAffected) +209<br />
  DataLayer.OnlineUsers.Update_SessionEnd_And_Online(Object Session_End,
  Boolean Online) +440<br />
  NiceFileExplorer.Global.Application_Start(Object sender, EventArgs e)
  +163</p>
  <p>[HttpException (0x80004005): Timeout expired.  The timeout period
  elapsed prior to completion of the operation or the server is not
  responding. The statement has been terminated.]<br />
  System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext
  context, HttpApplication app) +4052053<br />
  System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr
  appContext, HttpContext context, MethodInfo[] handlers) +191<br />
  System.Web.HttpApplication.InitSpecial(HttpApplicationState state,
  MethodInfo[] handlers, IntPtr appContext, HttpContext context) +352<br />
  System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr
  appContext, HttpContext context) +407<br />
  System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr
  appContext) +375</p>
  <p>[HttpException (0x80004005): Timeout expired.  The timeout period
  elapsed prior to completion of the operation or the server is not
  responding. The statement has been terminated.]<br />
  System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11686928
  System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
  +141    System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
  wr, HttpContext context) +4863749</p>
  </blockquote>
  <hr />
  <p><strong>EDIT AFTER ANSWERS:</strong><br />
  my <code>Application_Start</code> in <code>Global.asax</code> is like below:</p>
  <pre><code>protected void Application_Start(object sender, EventArgs e)
  {
      Application[&quot;OnlineUsers&quot;] = 0;
  
      OnlineUsers.Update_SessionEnd_And_Online(
          DateTime.Now,
          false);
  
      AddTask(&quot;DoStuff&quot;, 10);
  }
  </code></pre>
  <p>The stored procedure being called is:</p>
  <pre><code>ALTER Procedure [dbo].[sp_OnlineUsers_Update_SessionEnd_And_Online]
      @Session_End datetime,
      @Online bit
  As
  Begin
      Update OnlineUsers
      SET
          [Session_End] = @Session_End,
          [Online] = @Online
  
  End
  </code></pre>
  <p>I have two methods for getting online users:</p>
  <ol>
  <li>using <code>Application[&quot;OnlineUsers&quot;] = 0;</code></li>
  <li>the other one using database</li>
  </ol>
  <p>So, for method #2 I reset all OnlineUsers at <code>Application_Start</code>.   There are over 482,751 records in that table.</p>