title: “error: 19 - Physical connection is not usable” with OWIN access in Azure database
body: <p>I have tried all the other postings on the dreaded "error 19" and found that the few with answers do not apply or do not help, hence this new post. This is a very serious potential problem for all Azure+EF users.</p>
  
  <h2>First occurrence:</h2>
  
  <p>I am using the latest version of everything in a VS2013 EF6.1 Razor project (packages listed at the end). The database is hosted on SQL Azure.</p>
  
  <p>After running my webapp a few time (in a dev environment) I get this error: <code>A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)</code></p>
  
  <p>The line it dies on is always this:
  <img src="https://i.stack.imgur.com/w0g70.png" alt="enter image description here"></p>
  
  <p>I gather the error relates to connection pooling (and running out of connections), but I cannot spot a leak anywhere.</p>
  
  <p>As I access OWIN membership and other database features throughout the app I have a <code>DatabaseContoller</code> from which all other controllers inherit. This creates all the relevant components and disposes of them.</p>
  
  <h2>DatabaseController.cs</h2>
  
  <pre><code>[Authorize]
  public class DatabaseController : Controller
  {
      #region properties
      /// &lt;summary&gt;
      /// User manager - attached to application DB context
      /// &lt;/summary&gt;
      protected UserManager&lt;ApplicationUser&gt; UserManager { get; set; }
  
      /// &lt;summary&gt;
      /// Role manager - attached to application DB context
      /// &lt;/summary&gt;
      protected RoleManager&lt;IdentityRole&gt; RoleManager { get; set; }
  
      /// &lt;summary&gt;
      /// Application DB context
      /// &lt;/summary&gt;
      protected ApplicationDbContext ApplicationDbContext { get; set; }
  
      /// &lt;summary&gt;
      /// Database context used by most controllers
      /// &lt;/summary&gt;
      protected ApplicationEntities Context { get; set; }
      #endregion properties
  
      #region Constructors
      public DatabaseController()
      {
          this.Context = new ApplicationEntities ();
          this.ApplicationDbContext = new ApplicationDbContext();
          this.UserManager = new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(this.ApplicationDbContext));
          this.RoleManager = new RoleManager&lt;IdentityRole&gt;(new RoleStore&lt;IdentityRole&gt;(this.ApplicationDbContext));
          this.UserManager.UserValidator = new UserValidator&lt;ApplicationUser&gt;(UserManager) { AllowOnlyAlphanumericUserNames = false };
      }
      #endregion Constructors
  
      protected override void Dispose(bool disposing)
      {
          if (disposing)
          {
              if (UserManager != null)
              {
                  this.UserManager.Dispose();
                  this.UserManager = null;
              }
              if (this.RoleManager != null)
              {
                  this.RoleManager.Dispose();
                  this.RoleManager = null;
              }
              if (this.ApplicationDbContext != null)
              {
                  this.ApplicationDbContext.Dispose();
                  this.ApplicationDbContext = null;
              }
              if (this.Context != null)
              {
                  this.Context.Dispose();
                  this.Context = null;
              }
          }
          base.Dispose(disposing);
      }
  }
  </code></pre>
  
  <h2>Packages installed</h2>
  
  <pre><code>  &lt;package id="Antlr" version="3.5.0.2" targetFramework="net45" /&gt;
    &lt;package id="bootstrap" version="3.1.1" targetFramework="net45" /&gt;
    &lt;package id="EntityFramework" version="6.1.0" targetFramework="net45" /&gt;
    &lt;package id="jQuery" version="1.11.0" targetFramework="net45" /&gt;
    &lt;package id="jQuery.Validation" version="1.11.1" targetFramework="net45" /&gt;
    &lt;package id="json2" version="1.0.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Identity.Core" version="2.0.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Identity.EntityFramework" version="2.0.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Identity.Owin" version="2.0.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Mvc" version="5.1.1" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.WebApi" version="5.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.WebApi.Client" version="5.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.WebApi.Core" version="5.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.WebApi.WebHost" version="5.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security.Cookies" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security.Facebook" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security.Google" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security.MicrosoftAccount" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security.OAuth" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Owin.Security.Twitter" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /&gt;
    &lt;package id="Modernizr" version="2.7.2" targetFramework="net45" /&gt;
    &lt;package id="Newtonsoft.Json" version="6.0.2" targetFramework="net45" /&gt;
    &lt;package id="Owin" version="1.0" targetFramework="net45" /&gt;
    &lt;package id="Owin.Security.Providers" version="1.3.1" targetFramework="net45" /&gt;
    &lt;package id="Respond" version="1.4.2" targetFramework="net45" /&gt;
    &lt;package id="WebGrease" version="1.6.0" targetFramework="net45" /&gt;
  </code></pre>
  
  <p>Assuming it is a connection leak, how can I track down the source of the leak?</p>
  
  <p>If you need any more information, just ask.</p>
  
  <h2>Update: 22 May 2014 Second Bounty offered</h2>
  
  <p>I still have the same problem, with some slight project changes made since last posting, so will post latest details below shortly.</p>
  
  <p>I have added <code>Connection Lifetime=3;Max Pool Size=3;</code> to my connection strings, based on <a href="http://social.msdn.microsoft.com/Forums/en-US/89d4a844-180a-40df-ba48-b3dade9b310c/error-a-transportlevel-error-has-occurred-when-receiving-results-from-the-server-provider?forum=adodotnetentityframework" rel="noreferrer">this post</a>.</p>
  
  <h2>Update: 23 May 2014 Error still occurs</h2>
  
  <p>The next day, after debugging a few dozen times, this error returned.</p>
  
  <h2>Update: 11 June 2014</h2>
  
  <p>After 2 bounties and countless Google investigations (no real answer to this), I have to assume it is a flaw in Entity Framework 6, that I am somehow causing to appear.</p>
  
  <p><strong>More Information:</strong></p>
  
  <p>I just had the same error in a WinForm project, connect to Azure. In this instance I was accidentally not clearing an entity list after each 20 new items were added. </p>
  
  <p>Every time the code ran it added 20 more records and updated the <code>DateModified</code> field on all of them. By the time it hit 1700 records being updated it suddenly gave the dreaded "error 19 - Physical connection is not usable". After that I needed to restart my debug IIS for it to work at all. </p>
  
  <p>Obviously the code had run a massive number of updates, and maybe something about this will help <em>someone</em> think of <em>something</em>.</p>