超时已过期。在 Azure SQL 上执行操作之前已经超过了超时时间。

4

我需要在global.asax应用程序启动事件中创建一个Windows Azure的SQL数据库,但是我遇到了以下错误:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.  This failure occurred while attempting to connect to the routing destination. The duration spent while attempting to connect to the original server was - [Pre-Login] initialization=296; handshake=324; [Login] initialization=0; authentication=1; [Post-Login] complete=94;

我的代码如下:

   private void SetupSSM() {
            SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder
            {
                UserID = SettingsHelper.AzureUsernamedb,
                Password = SettingsHelper.AzurePasswordDb,
                ApplicationName = SettingsHelper.AzureApplicationName,
                DataSource = SettingsHelper.AzureSqlServer
            };

            bool created=DbUtils.CreateDatabaseIfNotExists(connStrBldr.ConnectionString, SettingsHelper.Azureshardmapmgrdb);
            if(created)
            {
                Sharding sharding = new Sharding(SettingsHelper.AzureSqlServer, SettingsHelper.Azureshardmapmgrdb, connStrBldr.ConnectionString);
            }
        }



  public static bool CreateDatabaseIfNotExists(string connectionString, string databaseName)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(
                    string.Format("SELECT * FROM sys.databases WHERE [name]=\'{0:S}\'", databaseName),
                    conn);

                if (cmd.ExecuteScalar() == null)
                {
                    SqlCommand cmd2 = new SqlCommand(
                        string.Format("CREATE DATABASE [{0:S}];", databaseName),
                        conn);

                    cmd2.ExecuteNonQuery();

                    return true;
                }
                else
                    return false;
            }
        }

我该如何增加超时时间?这是SQL超时还是由于SQL未响应导致的Web请求超时?

1
我这里也遇到了同样的问题。正在使用Azure搜索,并从SQL数据库导入数据。一段时间后总是超时。尝试增加超时时间,但没有成功。 - Anoyz
3个回答

7

2
实际上我遇到了相同的问题,我的SqlStatement.CommandTimeout设置为900秒,有时仍会引起问题。它似乎与Azure DB中的DTUs和记录数量(数据库大小)有关。基本5DTU 2GB DB选项似乎是最糟糕的。由于某些原因,某些存储过程和/或查询即使以前已经快速执行过,现在也只能挂起而没有明显的原因。我认为这可能是小型Azure DB的进程并发性问题。 - Milan
1
你应该检查 DTU 使用情况,并决定数据库的版本。 - Satya_MSFT
1
是的,我同意,但如果将数据库大小保持在限制范围内,基础级DTU(5)应该可以像高级级别DTU一样表现出色。 - Milan
DTU除了数据库大小之外还有其他因素,例如可用内存、CPU等。这是基于每秒事务数的。 - Satya_MSFT
我真的希望它能够如此简单,但例如现在我正在从几个数据库中的同一张表中删除100条记录。在13个数据库上,没有任何问题地删除了,而在剩下的2个数据库上仍然被锁定。所有数据库都是基本的5DTU,并且在任何DB或根服务器上都没有(用户基础的)并发进程运行。两个“冻结”的数据库的DTU%约为1.79%,DB大小为823MB。 - Milan
我使用基本级别的数据库也遇到了同样的问题。有解决方法吗? - Nash

4

当我们遇到这个问题时,发现我们的数据库性能不足。性能监视器显示在我们遇到错误时DTU已经达到最大值。

Azure SQL数据库操作超时

0

除了 @Satya_MSFT 的解释之外,如果您的驱动程序支持,您可以在连接字符串中添加命令超时。这可能看起来像这样(使用 ADO.NET 语法):

_connectionString = "Server=tcp:myenv.database.windows.net,1433;Database=mydb;User ID=myuserid;Password=mypassword;Encrypt=true;Connection Timeout=120;Command Timeout=120;"

然后可以在Microsoft.Data.SqlClient.SqlConnection中使用,例如:

await using var conn = new SqlConnection(_connectionString);

像其他人所说的那样,在你尝试这种方式之前,你应该尝试调整你的数据库。(并非在OPs情况下。如果这是一次性的设置操作,增加超时时间然后在之后减少它是可以的)


关键字“命令超时”不受支持 - hiFI

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接