【问题标题】:Azure - WebJob timeout when calling stored procedureAzure - 调用存储过程时 WebJob 超时
【发布时间】:2016-07-11 23:16:55
【问题描述】:

我有一个 Web 作业,它调用一个长时间运行的存储过程,该过程会一直超时。有人可以帮忙吗?

使用以下代码调用 Web 作业:

    static void Main()
    {
        ApplicationDbContext context = new ApplicationDbContext();

        context.Database.CommandTimeout = 6000;

        context.PopulateJobTypeDescendants();

    }

上下文(ApplicationDbContext)上的方法如下所示:

    public void PopulateJobTypeDescendants()
    {
        Database.ExecuteSqlCommand("PopulateJobTypeDescendants");
    }

运行 Web 作业时会引发以下异常。我们已经读到它可能与服务器上的计划/DTU 有关,所以我们从 S1 -> S3 开始,这仍然没有解决问题,并且进程在 45 秒后爆炸。奇怪的是,如果我从 SSMS 连接到 azure sql db 并调用存储过程,它工作正常。

[07/11/2016 22:25:02 > e2cf50: ERR ] 未处理的异常: System.Data.SqlClient.SqlException:超时已过期。超时 在操作完成之前经过的时间段或服务器处于 没有响应。尝试连接时发生此故障 路由目的地。尝试的持续时间 连接到原始服务器是 - [Pre-Login] 初始化 = 14; 握手=26; [登录] 初始化=0;身份验证=0; [登录后] 完成=1; ---> System.ComponentModel.Win32Exception:等待 操作超时

连接字符串如下所示:

<add name="TempsContext" connectionString="Server=tcp:[XXX],1433;Database=temps_testing;User ID=[XXX];Password=[XXX];Trusted_Connection=False;Encrypt=True;Connection Timeout=600;" providerName="System.Data.SqlClient" />

【问题讨论】:

    标签: sql-server azure azure-sql-database


    【解决方案1】:

    这可能是由于 EF 如何将CommandTimeout 的值传播到其创建的命令的一些不一致造成的,例如进行数据库初始化或从服务器获取版本信息。

    应该可以使用命令拦截作为解决方法,例如:

    using System.Data.Common;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure.Interception;
    
    namespace CommandTimeOutBug
    {
        class Program
        {
            static void Main(string[] args)
            {
                DbInterception.Add(new MyInterceptor());
    
                using (var context = new ApplicationDbContext())
                {
    
                    context.Database.CommandTimeout = 6000;
    
                    context.PopulateJobTypeDescendants();
                }
            }
        }
    
        public class ApplicationDbContext : DbContext
        {
    
            public void PopulateJobTypeDescendants()
            {
               Database.ExecuteSqlCommand("PopulateJobTypeDescendants");
            }
        }
    
        public class MyInterceptor: DbCommandInterceptor
        {
    
            public override void NonQueryExecuting(DbCommand command, 
                DbCommandInterceptionContext<int> interceptionContext)
            {
                command.CommandTimeout = 6000;
                base.NonQueryExecuting(command, interceptionContext);
            }
    
            public override void ReaderExecuting(DbCommand command, 
                DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                command.CommandTimeout = 6000;
                base.ReaderExecuting(command, interceptionContext);
            }
    
            public override void ScalarExecuting(DbCommand command, 
                DbCommandInterceptionContext<object> interceptionContext)
            {
                command.CommandTimeout = 6000;
                base.ScalarExecuting(command, interceptionContext);
            }
        }
    }
    

    我在https://github.com/aspnet/EntityFramework6/issues/24 创建了一个错误来跟踪这个问题。

    【讨论】:

      【解决方案2】:

      确实,这看起来像是一个性能问题。您能否在 micrososft dot com 的 mihaelab 上离线回复我,提供服务器、数据库名称详细信息和异常的完整调用堆栈?

      谢谢, 米哈埃拉

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        • 2011-02-11
        相关资源
        最近更新 更多