【问题标题】:Azure Function timer trigger failed runs execution of stored procedure [duplicate]Azure函数计时器触发器失败运行存储过程的执行[重复]
【发布时间】:2020-11-06 22:31:41
【问题描述】:

我想在我的天蓝色函数计时器触发器上执行存储过程。 功能部署成功。 但是,当函数运行时,我收到此错误堆栈跟踪消息

Microsoft.Azure.WebJobs.Host.FunctionInvocationException:
...
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw:
...
Inner exception System.ComponentModel.Win32Exception handled at System.Data.SqlClient.SqlConnection.OnError:
...

这是代码,

[FunctionName("DimensionFactTableUpdate")]
public static async Task Run([TimerTrigger("%CronJobSchedule%")]TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger UpdateFactDimensionTable function executed at: {DateTime.Now}");
    var _connectionString = Environment.GetEnvironmentVariable("DbConnectionString");
    var _storedProcedureDimension = Environment.GetEnvironmentVariable("StoredProcedureDimension");

    if (string.IsNullOrEmpty(_connectionString) || string.IsNullOrEmpty(_storedProcedureDimension))            
        return;            

    #region UPDATE DIMENSION TABLE
    try
    {
        log.Info($"[START] Update dimension table at: {DateTime.Now}");
        using (var connection = new SqlConnection(_connectionString))
        {
            connection.Open();
            using (var command = new SqlCommand(_storedProcedureDimension, connection) { CommandType = System.Data.CommandType.StoredProcedure })
            {
                var status = await command.ExecuteNonQueryAsync();
            }
        }
        log.Info($"[END] Update Dimension table at: {DateTime.Now}");
    }
    catch(Exception ex)
    {
        log.Error(ex.ToString());
    }            
    #endregion 
}

谢谢。

======

已编辑:

这是异常消息,

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

【问题讨论】:

  • 你能包括完整的例外吗? (异常的 ToString。)而不是堆栈跟踪的部分
  • 如果 connectionstringstoredprecuderdimension 变量不为空或不为空,则可以验证它们。
  • @PrebenHuybrechts 这是异常消息 > System.Data.SqlClient.SqlException (0x80131904):执行超时。在操作完成之前超时时间已过或服务器没有响应。谢谢

标签: c# .net sql-server azure-functions


【解决方案1】:

所以你收到了一个超时异常:

这可能意味着两件事。您无法登录到 SQL Server。 Open() 语句中是否出现此异常?
然后调查登录问题:

  • 您的连接字符串是否正确?
  • 您的数据库(tcp 和端口)是否可以从 azure 访问? (如果您使用的是 Azure SQL 数据库,这没关系)
  • 登录名是否具有正确的权限?
  • 查看 SQL Server 错误日志sp_readerrorlog 了解登录失败原因的详细信息。

否则,如果您在 ExecuteNonQueryAsync 收到异常,则表示超时。

选项 1:简单的方法,但不一定是最好的

增加SqlCommandlike 的超时时间

command.CommandTimeout = 60; // 60 seconds = 1 Minute

选项 2:代价高昂的方式

横向扩展您的 SQL Server 数据库(假设您使用的是 Azure SQL)

选项3:最耗时的方式

调查为什么你的存储过程很慢,并解决这个问题。 这可能有很多问题,阻塞、等待、索引不佳或没有索引、游标/while、...

【讨论】:

  • 登录时未发现问题。
  • 选项 2 效果很好。但可以肯定的是,我需要改进我的存储过程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 2019-07-22
  • 1970-01-01
  • 2022-10-19
相关资源
最近更新 更多