简体   繁体   English

Azure SQL连接正在超时

[英]Azure SQL Connections are Timing Out

Ok, in an attempt to move my app to the cloud I've moved a local SQL database to Azure SQL. 好的,为了将我的应用程序移动到云中,我已经将本地SQL数据库移动到了Azure SQL。 The problem is that the connection to that new Azure SQL database is so 'flakey' I'm about to bring it back in house. 问题在于,与该新Azure SQL数据库的连接是如此“不稳定”,我将其带回内部。

The task is to loop and create aa total of about 481K records in the database. 任务是循环并在数据库中创建总共约481K记录。

The connection string is 连接字符串是

"Server=tcp:xxx,1433;Initial Catalog=xx;Persist Security Info=False;User ID=xx;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;ConnectRetryCount=255;"

The SQL query it is running each time is not complicated. 每次运行的SQL查询并不复杂。 Just inserting three values into three columns. 只需将三个值插入三列即可。 (column and values changed to protect some internal workings) (更改了列和值以保护某些内部工作)

Insert Into TheTable (C1, C2, C3) VALUES ('V1', 'V2', 'V3')

but at random points it throws this. 但在随机点上会抛出该错误。

System.ComponentModel.Win32Exception (0x80004005): The wait operation timed outExecution Timeout Expired. System.ComponentModel.Win32Exception(0x80004005):等待操作超时执行超时超时。 The timeout period elapsed prior to completion of the operation or the server is not responding. 在操作完成之前超时或服务器没有响应。 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at XX in D:\\PATHOFTHEFILE:line 420 在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning( 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource异常,布尔值BreakConnection,操作1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1完成,字符串methodName,布尔sendToPipe,Int32超时,布尔&usedCache,布尔asyncWrite,布尔inRetry)位于XX的D:\\ PATHOFTHEFILE:line 420处

Note that 注意

1) I'm opening and closing the connection each time I create a record and closing it in the next step. 1)每次创建记录并在下一步中将其关闭时,我都会打开和关闭连接。

2) There's no one else hitting the database except me. 2)除我之​​外,没有其他人可以访问数据库。

3) The database service is set for S1. 3)为S1设置数据库服务。

4) Yeah - I get the irony that the program is crashing on line 420. I'm trying to find ways to drug test the code. 4)是的-具有讽刺意味的是,该程序在420行崩溃。我正在尝试寻找对代码进行药物测试的方法。

Questions 问题

1) Is there anything wrong with my connection string? 1)我的连接字符串有什么问题吗? The documentation says that I should use a Timeout of 30 when connecting to an Azure SQL database. 该文档说,连接到Azure SQL数据库时应使用30的超时。 Frankly the code ran better (or at least lived longer) when I had the timeout set for 0. 坦白说,当我将超时设置为0时,代码运行得更好(或至少寿命更长)。

2) At first I tried using a single connection to handle the loop through the entire 481K INSERT statements. 2)首先,我尝试使用单个连接来处理整个481K INSERT语句的循环。 Is that a bad design? 设计不好吗? How long will Azure SQL reliability hold a connection? Azure SQL可靠性将保持连接多长时间?

3) I'm not getting a warm fuzzy feeling about the ability to build rock solid apps on Azure SQL. 3)对于在Azure SQL上构建坚如磐石的应用程序的能力,我并没有感到困惑。 Can someone point me to a good reference about the difference between building for local SQL vs Azure SQL. 有人可以为我提供有关构建本地SQL与Azure SQL之间的区别的很好的参考。 I've gone through everything I can find and there just didn't seem to be that much out there. 我已经找到了所有可以找到的东西,但似乎没有那么多。

4) I like the fact that I can connect to Azure SQL with MMC. 4)我喜欢可以通过MMC连接到Azure SQL的事实。 But there are (generically speaking) all kinds of monitoring info I can't get from MMC anymore. 但是(从一般意义上来说)有我无法从MMC获得的各种监视信息。 Anyone have a link to something that can help me really understand what's going on in the database without using that dreadful Azure Portal 任何人都有指向某些内容的链接,这些链接可以帮助我真正了解数据库中正在发生的事情,而无需使用该可怕的Azure Portal

UPDATE #1 更新#1

Guilty as charged 被控有罪

public static void RunSQL(string SQLString)
        {

        int a = 0;

        SqlCommand Command = new SqlCommand(SQLString, TheConnection);
        try
            {
            a = Command.ExecuteNonQuery();
            }
        catch (Exception ex)
            {

            Notifications.EventLogging.ProcessEvent(SQLString + " go boom " + ex.InnerException + ex.Message + ex.StackTrace);
            Notifications.EventLogging.ProcessEvent("Time Of Death" + DateTime.Now);
            Console.ReadKey();

            }

Azure SQL instances are hosted on shared infrastructure. Azure SQL实例托管在共享基础结构上。 Azure will throttle requests to ensure that all instances on a server can meet the minimum SLA. Azure将限制请求,以确保服务器上的所有实例都可以满足最低SLA。 In this life, death and taxes are guaranteed, but Azure SQL Connections are not. 在这一生中,可以保证死亡和税收,但不能保证Azure SQL连接。

To deal with this, you need to have automatic retry. 要解决此问题,您需要自动重试。 Microsoft has provided a few options over the years, beginning with the now deprecated ReliableSqlConnection class. 多年来,Microsoft提供了一些选项,从现已弃用的ReliableSqlConnection类开始。 The preferred way to talk to Azure SQL these days is with Entity Framework 6.x, which has automatic retry built in. 目前,与Azure SQL进行通讯的首选方法是使用Entity Framework 6.x,该工具内置了自动重试功能。

In practice, an Azure SQL database that sees light and sporadic traffic rarely sees a throttle event. 实际上,看到少量零星流量的Azure SQL数据库很少会遇到限制事件。 I've had developer friends who have deployed production code hitting an Azure SQL database, used raw SqlConnections and SqlCommands, and seemed genuinely surprised when I told them that connections aren't guaranteed. 我有一些开发人员朋友,他们已经部署了生产代码并访问了Azure SQL数据库,使用了原始SqlConnections和SqlCommands,当我告诉他们不能保证连接时,我真的感到很惊讶。 They'd never run across it! 他们永远不会碰到它! But if you're hitting the hell out of a server (as you are doing), I've seen it happen enough to be noticeable. 但是,如果您正在使用服务器(如您所做的那样),那么,它已经足够引起人们的注意了。

EDIT 12-6-2018 : I have experienced this problem myself in the last few months. 编辑12-6-2018 :在过去几个月中,我自己遇到了这个问题。 After combing through the logs, the culprit was spikes in database traffic that maxed out the DTU limit for my SQL Server. 梳理日志后,罪魁祸首是数据库流量激增,从而使我的SQL Server的DTU限制最大化。 Retry is not necessarily a sufficient solution in this case because automatic retry effectively helps choke your SQL DB. 在这种情况下,重试不一定是足够的解决方案,因为自动重试可以有效地帮助您阻止SQL DB。 To check and see if you are falling victim to DTU throttling, go to your Azure SQL DB's Overview tab, look at the resource utilization graph, and make SURE you select Max as the metric. 若要检查并查看是否成为DTU节流的受害者,请转到Azure SQL DB的“概述”选项卡,查看资源利用率图,并确保您选择“最大”作为指标。 It defaults to Avg, and this can hide spikes in DTU traffic. 默认为平均,这可以隐藏DTU流量中的峰值。

Edit 8-6-2019 : If you're maxing out DTUs and want to know what's causing it, there are a few places to look on the Azure SQL Management blade in the Azure Portal: 编辑8-6-2019 :如果您要最大限度地利用DTU,并且想知道是什么原因造成的,则可以在Azure门户中的Azure SQL管理刀片上找到一些地方:

  1. Query performance insights. 查询性能见解。 You can use the tools here to find your top resource-consuming queries that have run in a given time period. 您可以使用此处的工具查找在给定时间段内运行的最消耗资源的查询。 This is a good place to start. 这是一个很好的起点。 Make sure to check through all metrics. 确保检查所有指标。
  2. Performance recommendations. 性能建议。 If you're missing an index, it's probably listed here. 如果您缺少索引,则可能在此处列出。
  3. Metrics. 指标。 Check through all listed. 检查所有列出的内容。 Make sure to set your aggregation to Max. 确保将汇总设置为最大。

This should give you good (or even great) indicators of what's going wrong. 这应该为您提供正确的(或什至很棒的)错误指示。

Upgrade to higher level of Premium version, migrate and then downgrade accordingly. 升级到更高级别的高级版,进行迁移,然后相应地降级。 This will help you get around timeout errors. 这将帮助您解决超时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM