title: Slow Database Connection from Azure Web Application
body: <p>I have developed a Web Application a standard web application to allow users to display and update a set of data from an SQL database. </p>
  
  <p>The Web Application uses a AngularJS client side which interacts with the Web Server via MVC Web API calls to retrieve and update data on the database.
  The Server side code is written in C# using .NET 4.5 and uses Entity Framework v6.0 to access the database.</p>
  
  <p>The Web Application is hosted in an Azure Web App.
  The Database is the Azure SQL Database.</p>
  
  <p>The issue is that when the Application has not been used for about 10-15 minutes, then it is used again, the first data retrieval often takes over 10 seconds to  return to the browser. After that the performance is fine until the next time the application is left unused.</p>
  
  <p>I've put trace in the application and we see that the delay is when the connection opens. The actual query on the database runs sub-second.</p>
  
  <p>I've noticed though that with different hosting configurations I get different results. In particular hosting in house and pointing to the Azure database does not encounter anywhere near the same delays.</p>
  
  <p>I've changed one of the routines to use ADO.NET instead of Entity Framework and changed the trace to try to narrow it down further.</p>
  
  <p>What I see is this:</p>
  
  <pre><code>ConnectionStringSettings ADOcnxstring = ConfigurationManager.ConnectionStrings["DevFEConnectAdo"];
  DbConnection ADOconnection = new SqlConnection(ADOcnxstring.ConnectionString);
  </code></pre>
  
  <p>The delay is here (before the SQL has even been defined!</p>
  
  <p>and then I build the command and do the DataReader etc:</p>
  
  <pre><code>    DbCommand ADOcommand = ADOconnection.CreateCommand();
               :
  etc
  </code></pre>
  
  <p>So the delay is on opening the Connection to the database.</p>
  
  <p>My connection string is standard:</p>
  
  <pre><code>&lt;add name="DevFEConnectAdo" connectionString="data   
  source=feeunsqldevfeconnect.database.windows.net;initial 
  catalog=feeunsqldbdevfeconnect;persist security info=True;user id=???
  @???;password=???;multipleactiveresultsets=True"&gt;&lt;/add&gt;
  </code></pre>