简体   繁体   English

Azure - SqlBulkCopy 抛出超时过期异常

[英]Azure - SqlBulkCopy throwing a timeout expired exception

I'm using an azure sql database (v12) on a vm.我在虚拟机上使用 azure sql 数据库 (v12)。 I have two different databases instances - one for staging and one for production.我有两个不同的数据库实例 - 一个用于登台,一个用于生产。 I'm trying to grab the data off of staging and insert it into production with the click of a button.我正在尝试从暂存中获取数据,然后单击按钮将其插入到生产中。 This code works successfully 'sometimes' meaning randomly it will be successful.此代码“有时”成功运行,这意味着它会随机成功。 Otherwise I'm getting back an error of:否则我会返回以下错误:

BULK COPY Commit Exception Type: {0}System.Data.SqlClient.SqlException BULK COPY Message: {0}Timeout expired. BULK COPY 提交异常类型:{0}System.Data.SqlClient.SqlException BULK COPY 消息:{0}超时已过期。 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=1;尝试连接到原始服务器所花费的时间为 - [Pre-Login] 初始化=1; handshake=17;握手=17; [Login] initialization=0; 【登录】初始化=0; authentication=0;认证=0; [Post-Login] complete=0; 【登录后】完成=0;

Here is the code that I'm using to accomplish this task, maybe there's a flaw that I'm not seeing.这是我用来完成此任务的代码,也许存在我没有看到的缺陷。 By dump the StringBuilder out I can see that the SELECT query works and the DELETE query works but the error is thrown when I attempt to copy the data using SqlBulkCopy.通过转储 StringBuilder,我可以看到 SELECT 查询有效,DELETE 查询有效,但是当我尝试使用 SqlBulkCopy 复制数据时抛出错误。 Any help would be greatly appreciated.任何帮助将不胜感激。 I've gone through a bunch of the MSDN docs already with no luck -> adding longer CommandTimeouts, adding a longer BulkCopyTimeout, and re-configuring ports on my firewall.我已经浏览了一堆 MSDN 文档,但没有运气-> 添加更长的 CommandTimeouts,添加更长的 BulkCopyTimeout,并在我的防火墙上重新配置端口。 Still no luck.仍然没有运气。

Resources I've used: https://social.msdn.microsoft.com/Forums/en-US/1467d64f-69ae-4c1f-91a2-349fc5d514ae/sqlbulkcopy-fails-with-timeout-expired-error?forum=adodotnetdataproviders我使用过的资源: https : //social.msdn.microsoft.com/Forums/en-US/1467d64f-69ae-4c1f-91a2-349fc5d514ae/sqlbulkcopy-fails-with-timeout-expired-error?forum=adodotnetdataproviders

https://azure.microsoft.com/nb-no/documentation/articles/sql-database-develop-direct-route-ports-adonet-v12/ https://azure.microsoft.com/nb-no/documentation/articles/sql-database-develop-direct-route-ports-adonet-v12/

Timeout expired with SqlBulkCopy SqlBulkCopy 超时

public static object SyncData()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Internal Connection...");
    string internalConnectionString = GetConnectionString("ConnectionString");
    using (SqlConnection internalConnection = new SqlConnection(internalConnectionString))
    {
        internalConnection.Open();              
        SqlCommand selectCommand = internalConnection.CreateCommand();
        selectCommand.CommandTimeout = 180;
        try
        {
            selectCommand.CommandText = "SELECT * FROM dbo.test";
            SqlDataReader reader = selectCommand.ExecuteReader();

            sb.AppendLine("External Connection...");
            string externalConnectionString = GetConnectionString("ExternalConnectionString");
            using (SqlConnection externalConnection = new SqlConnection(externalConnectionString))
            {
                externalConnection.Open();              
                SqlCommand CRUDCommand = externalConnection.CreateCommand();
                CRUDCommand.CommandTimeout = 180;
                SqlTransaction transaction = externalConnection.BeginTransaction("test");
                CRUDCommand.Connection = externalConnection;
                CRUDCommand.Transaction = transaction;
                try
                {
                    CRUDCommand.CommandText = "DELETE FROM dbo.test";
                    sb.AppendLine("DELETE: Number of rows affected = " + CRUDCommand.ExecuteNonQuery());
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(externalConnection, SqlBulkCopyOptions.KeepIdentity, transaction))
                    {
                        try
                        {
                            bulkCopy.DestinationTableName = "dbo.test";
                            bulkCopy.BatchSize = 100;
                            bulkCopy.BulkCopyTimeout = 180;
                            bulkCopy.WriteToServer(reader);

                            sb.AppendLine("Table data copied successfully");

                            transaction.Commit();
                            sb.AppendLine("Transaction committed.");
                        }
                        catch (Exception ex)
                        {
                            sb.AppendLine("BULK COPY Commit Exception Type: {0}" + ex.GetType());
                            sb.AppendLine("  BULK COPY Message: {0}" + ex.Message);
                            try
                            {
                                transaction.Rollback();
                            }
                            catch (Exception ex2)
                            {
                                sb.AppendLine("Rollback Exception Type: {0}" + ex2.GetType());
                                sb.AppendLine("  Message: {0}" + ex2.Message);
                            }
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    sb.AppendLine("Commit Exception Type: {0}" + ex.GetType());
                    sb.AppendLine("  Message: {0}" + ex.Message);

                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        sb.AppendLine("Rollback Exception Type: {0}" + ex2.GetType());
                        sb.AppendLine("  Message: {0}" + ex2.Message);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            sb.AppendLine("Commit Exception Type: {0}" + ex.GetType());
            sb.AppendLine("  Message: {0}" + ex.Message);
        }
    }
    return sb.ToString();
}

When creating your SqlBulkCopy instance, you're passing the connection string externalConnectionString and thus opening a new connection.创建 SqlBulkCopy 实例时,您将传递连接字符串externalConnectionString并因此打开一个新连接。 That may be causing a deadlock issue with both connections trying to modify the same table.这可能会导致两个连接尝试修改同一个表时出现死锁问题。

Have you tried passing your existing connection externalConnection to the SqlBulkCopy constructor instead of the connection string?您是否尝试过将现有连接externalConnection传递给 SqlBulkCopy 构造函数而不是连接字符串?

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

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