"错误:19 - 物理连接不可用"与Azure数据库中的OWIN访问

Gon*_*ing 28 c# connection-pooling razor owin azure-sql-database

我已经在可怕的"错误19"上尝试了所有其他帖子,并发现少数答案不适用或没有帮助,因此这篇新帖子.对于所有Azure + EF用户来说,这是一个非常严重的潜在问题.

第一次出现:

我正在使用VS2013 EF6.1 Razor项目中的所有内容的最新版本(最后列出的软件包).该数据库托管在SQL Azure上.

运行我的webapp几次后(在开发环境中)我收到此错误: A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)

它死的线总是如此: 在此输入图像描述

我收集错误与连接池(和连接用完)有关,但我无法在任何地方发现泄漏.

当我访问整个应用程序中的OWIN成员资格和其他数据库功能时,我有一个DatabaseContoller所有其他控制器继承的.这将创建所有相关组件并处理它们.

DatabaseController.cs

[Authorize]
public class DatabaseController : Controller
{
    #region properties
    /// <summary>
    /// User manager - attached to application DB context
    /// </summary>
    protected UserManager<ApplicationUser> UserManager { get; set; }

    /// <summary>
    /// Role manager - attached to application DB context
    /// </summary>
    protected RoleManager<IdentityRole> RoleManager { get; set; }

    /// <summary>
    /// Application DB context
    /// </summary>
    protected ApplicationDbContext ApplicationDbContext { get; set; }

    /// <summary>
    /// Database context used by most controllers
    /// </summary>
    protected ApplicationEntities Context { get; set; }
    #endregion properties

    #region Constructors
    public DatabaseController()
    {
        this.Context = new ApplicationEntities ();
        this.ApplicationDbContext = new ApplicationDbContext();
        this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.ApplicationDbContext));
        this.RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this.ApplicationDbContext));
        this.UserManager.UserValidator = new UserValidator<ApplicationUser>(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);
    }
}
Run Code Online (Sandbox Code Playgroud)

已安装包

  <package id="Antlr" version="3.5.0.2" targetFramework="net45" />
  <package id="bootstrap" version="3.1.1" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.0" targetFramework="net45" />
  <package id="jQuery" version="1.11.0" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
  <package id="json2" version="1.0.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.1.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="5.1.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.1.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.1.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.1.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.1.2" targetFramework="net45" />
  <package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Cookies" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Facebook" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Google" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.OAuth" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Twitter" version="2.1.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="2.7.2" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.2" targetFramework="net45" />
  <package id="Owin" version="1.0" targetFramework="net45" />
  <package id="Owin.Security.Providers" version="1.3.1" targetFramework="net45" />
  <package id="Respond" version="1.4.2" targetFramework="net45" />
  <package id="WebGrease" version="1.6.0" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)

假设是连接泄漏,我该如何追踪泄漏源?

如果您需要更多信息,请询问.

更新:2014年5月22日提供第二笔赏金

我仍然遇到同样的问题,自上次发布以来进行了一些轻微的项目更改,因此将在下面发布最新的详细信息.

我已Connection Lifetime=3;Max Pool Size=3;根据这篇文章添加到我的连接字符串中.

更新:2014年5月23日错误仍然发生

第二天,经过几十次调试后,返回了此错误.

更新:2014年6月11日

经过2次赏金和无数谷歌调查(对此没有真正的答案),我不得不假设它是实体框架6中的一个缺陷,我在某种程度上导致出现.

更多信息:

我在WinForm项目中遇到了同样的错误,连接到Azure.在这种情况下,我添加了每20个新项目后意外没有清除实体列表.

每次代码运行时,它都会添加20个记录并更新DateModified所有记录.当它达到1700条记录被更新时,它突然发出了可怕的"错误19 - 物理连接不可用".之后,我需要重新启动我的调试IIS才能使其工作.

显然,代码已经运行了大量的更新,也许有关此问题的内容将有助于某人想到某些事情.

Gon*_*ing 6

错误 19不是通信错误!(或不仅仅是通讯错误)

只需确保您.Include(x=>x.ForeignTable)的 LINQ to SQL 查询中有所有必需的调用!

2015 年 8 月更新(至少在某些情况下可能的解决方案):

我们刚刚有一个关于这个问题的 100% 重现案例,我们能够通过反复试验来解决,所以它很可能是一个解决方案,或者至少提供了寻找什么的线索。

设想:

  • 该错误仅在 IIS 下运行的发布版本下发生。它不会在调试或 IIS Express 下发生。
  • 我们还打开了 SQL 分析以查看服务器实际被击中的时间/地点。
  • 有问题的查询是获取匹配的记录,然后foreach在结果的迭代中创建视图模型(即惰性求值)。视图模型依赖于被查询实体的相关表中的值。

测试:

第一次尝试:删除查询中的任何复杂过滤器

  • 结果:仍然失败,错误 19

第二次尝试:添加ToList()到查询以强制查询立即运行完成

  • 结果:成功!!!(显然这里发生了一些事情)

第三次尝试:删除ToList()并添加.Include(x=>x.ForeignTable)到查询以强制包含相关数据。

  • 结果: 成功

我的新理论是:

如果您不小心遗漏了Include一个外部表,EF 将在延迟评估时随机无法获取相关数据。这会导致臭名昭著的错误 19。

由于识别框架中存在外键关系,您可能会假设.Include()OWIN 中某处的查询也缺少或等效的 。这可能会导致使用 OWIN 或其他查询时出现随机问题。

笔记:

  • 需要注意的一个关键点是错误 19不是通信错误。查询确实命中了 SQL 服务器。这是客户端无法获取相关数据的问题。

暂停掌声(我们很高兴找到这个) :)

2015 年 8 月 28 日更新:

今天再次遇到可怕的错误 19,连接到本地 SQL 数据库(通常这对我来说是 Azure 的问题)。根据上面的结果,我只是.Include(x=>x.ForeignTable)在适当的地方添加了一个语句,问题就消失了!这似乎是 EF 并不总是能够延迟加载相关表信息的问题。