Azure SQL 数据库有时无法从 Azure 网站访问
Azure SQL Database sometimes unreachable from Azure Websites
我有一个 asp.net 应用程序部署到连接到 Azure SQL 数据库的 Azure 网站。这在去年一直运行良好,但上周末我开始在连接到数据库时出现错误,并给出以下堆栈跟踪。
[Win32Exception (0x80004005): Access is denied]
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
此错误出现并持续几个小时,然后消失几个小时。数据库总是可以从我的机器访问。
我尝试过的一些事情是:
- 添加 "allow all" 防火墙规则 (0.0.0.0-255.255.255.255) 无效
- 更改连接字符串以使用数据库所有者作为凭据无效。
- 有什么效果是将 azure 托管级别更改为其他级别。这暂时解决了问题,网站可以再访问数据库几个小时。
可能是什么原因导致此错误开始出现?自 8 月以来应用程序未更改。
编辑:
Azure 支持发现实例上存在套接字和端口耗尽。根本原因是什么,目前还不得而知。
Craig 是正确的,您需要实施 SQLAzure 瞬态故障处理。您可以在此处找到说明:https://msdn.microsoft.com/en-us/library/hh680899(v=pandp.50).aspx
来自文章
You can instantiate a PolicyRetry object and wrap the calls that you
make to SQL Azure using the ExecuteAction method using the methods
show in the previous topics. However, the block also includes direct
support for working with SQL Azure through the ReliableSqlConnection
class.
The following code snippet shows an example of how to open a reliable
connection to SQL Azure.
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
...
// Get an instance of the RetryManager class.
var retryManager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();
// Create a retry policy that uses a default retry strategy from the
// configuration.
var retryPolicy = retryManager.GetDefaultSqlConnectionRetryPolicy();
using (ReliableSqlConnection conn =
new ReliableSqlConnection(connString, retryPolicy))
{
// Attempt to open a connection using the retry policy specified
// when the constructor is invoked.
conn.Open();
// ... execute SQL queries against this connection ...
}
以下代码片段显示了如何执行 SQL 命令并重试的示例。
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using System.Data;
...
using (ReliableSqlConnection conn = new ReliableSqlConnection(connString, retryPolicy))
{
conn.Open();
IDbCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText =
"UPDATE Application SET [DateUpdated] = getdate()";
// Execute the above query using a retry-aware ExecuteCommand method which
// will automatically retry if the query has failed (or connection was
// dropped).
int recordsAffected = conn.ExecuteCommand(selectCommand, retryPolicy);
}
Azure 支持的回答表明我遇到的连接问题是由于 port/socket 耗尽。这可能是由同一托管计划中的另一个网站造成的。
关于为什么通过更改托管服务级别消除症状的一些答案:
- 更改托管计划在一段时间内有所帮助,因为这会移动虚拟机并关闭所有套接字。
- 将托管计划从 B 级更改为 S 级有所帮助,因为 Azure 限制了 B 级上的套接字数量。
我有一个 asp.net 应用程序部署到连接到 Azure SQL 数据库的 Azure 网站。这在去年一直运行良好,但上周末我开始在连接到数据库时出现错误,并给出以下堆栈跟踪。
[Win32Exception (0x80004005): Access is denied]
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
此错误出现并持续几个小时,然后消失几个小时。数据库总是可以从我的机器访问。
我尝试过的一些事情是:
- 添加 "allow all" 防火墙规则 (0.0.0.0-255.255.255.255) 无效
- 更改连接字符串以使用数据库所有者作为凭据无效。
- 有什么效果是将 azure 托管级别更改为其他级别。这暂时解决了问题,网站可以再访问数据库几个小时。
可能是什么原因导致此错误开始出现?自 8 月以来应用程序未更改。
编辑: Azure 支持发现实例上存在套接字和端口耗尽。根本原因是什么,目前还不得而知。
Craig 是正确的,您需要实施 SQLAzure 瞬态故障处理。您可以在此处找到说明:https://msdn.microsoft.com/en-us/library/hh680899(v=pandp.50).aspx
来自文章
You can instantiate a PolicyRetry object and wrap the calls that you make to SQL Azure using the ExecuteAction method using the methods show in the previous topics. However, the block also includes direct support for working with SQL Azure through the ReliableSqlConnection class.
The following code snippet shows an example of how to open a reliable connection to SQL Azure.
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
...
// Get an instance of the RetryManager class.
var retryManager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();
// Create a retry policy that uses a default retry strategy from the
// configuration.
var retryPolicy = retryManager.GetDefaultSqlConnectionRetryPolicy();
using (ReliableSqlConnection conn =
new ReliableSqlConnection(connString, retryPolicy))
{
// Attempt to open a connection using the retry policy specified
// when the constructor is invoked.
conn.Open();
// ... execute SQL queries against this connection ...
}
以下代码片段显示了如何执行 SQL 命令并重试的示例。
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using System.Data;
...
using (ReliableSqlConnection conn = new ReliableSqlConnection(connString, retryPolicy))
{
conn.Open();
IDbCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText =
"UPDATE Application SET [DateUpdated] = getdate()";
// Execute the above query using a retry-aware ExecuteCommand method which
// will automatically retry if the query has failed (or connection was
// dropped).
int recordsAffected = conn.ExecuteCommand(selectCommand, retryPolicy);
}
Azure 支持的回答表明我遇到的连接问题是由于 port/socket 耗尽。这可能是由同一托管计划中的另一个网站造成的。
关于为什么通过更改托管服务级别消除症状的一些答案:
- 更改托管计划在一段时间内有所帮助,因为这会移动虚拟机并关闭所有套接字。
- 将托管计划从 B 级更改为 S 级有所帮助,因为 Azure 限制了 B 级上的套接字数量。