SQL Azure中的内部。net框架数据提供程序错误6

时间:2022-12-27 02:14:02

I regularly experience the above error when creating connections to Azure SQL databases. I've implemented ReliableSqlConnection with retry logic in attempt to avoid this issue but it has been to no avail. Following is an example error stack trace:

在创建到Azure SQL数据库的连接时,我经常遇到上述错误。我已经使用重试逻辑实现了ReliableSqlConnection,试图避免这个问题,但它没有任何作用。下面是一个错误堆栈跟踪示例:

System.InvalidOperationException
Internal .Net Framework Data Provider error 6. 
System.InvalidOperationException: Internal .Net Framework Data Provider error 6. 
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ReliableSqlConnection.<Open>b__1()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.<>c__DisplayClass1.<ExecuteAction>b__0()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.ExecuteAction[TResult](Func`1 func)
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.ExecuteAction(Action action)
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ReliableSqlConnection.<Open>b__0()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.<>c__DisplayClass1.<ExecuteAction>b__0()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.ExecuteAction[TResult](Func`1 func)
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.ExecuteAction(Action action)
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ReliableSqlConnection.Open(RetryPolicy retryPolicy)

This issue happens when creating a new database in an elastic pool. The SQL command text is execute in the following method:

当在弹性池中创建新数据库时,会发生此问题。SQL命令文本的执行方式如下:

public void ExecuteCommandText(string commandText)
    {
        if (IsNullOrEmpty(commandText))
            throw new ArgumentNullException(nameof(commandText));

        List<string> commandSteps = SplitCommandText(commandText);

        using (var sqlConnection = CreateConnection())
        {
            foreach (string commandStep in commandSteps)
            {
                using (SqlCommand command = sqlConnection.CreateCommand())
                {
                    command.CommandText = commandStep;
                    command.CommandTimeout = _commandTimeout;
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
        }
    }

Where:

地点:

private ReliableSqlConnection CreateConnection()
    {
        if (IsNullOrEmpty(ConnectionString))
            throw new InvalidOperationException("Connection string is not defined.");

        ReliableSqlConnection sqlConnection = new ReliableSqlConnection(ConnectionString, _retryPolicy, _retryPolicy);
        sqlConnection.Open();
        return sqlConnection;
    }

And:

和:

var retryStrategy = new ExponentialBackoff(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(2));
_retryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(retryStrategy);

The connection string is of the format:

连接字符串的格式为:

$"Server=tcp:{serverName},1433;Data Source={serverName};Persist Security Info=False;User ID='{user}';Password='{password}';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=90;"

Also, I've checked the eDTU usage on my elastic pool:

另外,我检查了我的弹性池的eDTU用法:

Elastic pool eDTU usage

弹性池eDTU用法

The first spike is from creating 1 database in the pool, the second is for 2, the third is for 3 and the fourth is for creating 4 databases concurrently. The elastic pool service tier is Standard 100.

第一个峰值来自于在池中创建一个数据库,第二个峰值来自于2,第三个峰值来自于3,第四个峰值来自于并发地创建4个数据库。弹性池服务层为标准100。

1 个解决方案

#1


1  

This is one of Azure SQL transient error. A lot of info is here https://msdn.microsoft.com/en-us/library/dn440719(v=pandp.60).aspx. Seem the same error when trying to do backups via code. In those cases I drop and re-initiate.

这是Azure SQL瞬变错误之一。很多信息都在这里,https://msdn.microsoft.com/en-us/library/dn440719(v=pandp.60).aspx。当尝试通过代码进行备份时,似乎也有相同的错误。在这些情况下,我放弃并重新开始。

#1


1  

This is one of Azure SQL transient error. A lot of info is here https://msdn.microsoft.com/en-us/library/dn440719(v=pandp.60).aspx. Seem the same error when trying to do backups via code. In those cases I drop and re-initiate.

这是Azure SQL瞬变错误之一。很多信息都在这里,https://msdn.microsoft.com/en-us/library/dn440719(v=pandp.60).aspx。当尝试通过代码进行备份时,似乎也有相同的错误。在这些情况下,我放弃并重新开始。