【问题标题】:Table adapter update in transaction scope keeps timing out事务范围内的表适配器更新不断超时
【发布时间】:2011-05-18 18:31:29
【问题描述】:

所以我有一个类型化的数据集,我从另一个数据库创建了记录(超过 500,000 条记录!)。我需要将所有这些记录导入另一个数据库并具有以下代码(减去初始化、添加行等):

try
{
    Console.WriteLine("Time to process of adding to table: Start at " + startDate.ToString() + " | End at " + DateTime.Now.ToString());

    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(1, 0, 0)))
    {
        laborTicketTableAdapter.Update(laborTicket);
        ts.Complete();
    }
    Console.WriteLine("Time to process transaction: Start at " + startDate.ToString() + " | End at " + DateTime.Now.ToString());
}
catch (SqlException ex)
{
    MessageBox.Show("Something bad happened" + Environment.NewLine + ex.StackTrace);
    result = false;
}
catch (Exception ex)
{
    MessageBox.Show("Something bad happened" + Environment.NewLine + ex.StackTrace);
    result = false;
}

控制台输出以下内容:

Time to process of adding to table: Start at 1/1/2009 2:05:59 PM | End at 5/18/2011 2:06:30 PM
The thread '<No Name>' (0xa5c) has exited with code 0 (0x0).
The thread '<No Name>' (0x2e4) has exited with code 0 (0x0).
The thread '<No Name>' (0xae0) has exited with code 0 (0x0).
A first chance exception of type 'System.Transactions.TransactionAbortedException' occurred in System.Transactions.dll

它抛出一个异常,内部异常是“事务超时”。事务已过期,因此我将超时设置为一小时,但在 15 分钟后仍然超时。怎么回事?

最后处理时间是下午 2:19:40,运行大约需要 15 分钟。我怎么会得到这个异常?我在控制台上看到三个线程,但这是一个空白项目中的空白。所有其他连接都关闭到外部数据源,当我进行更新时只剩下那个表适配器和表对象。

-- 编辑-- Machine.config 没有 maxTimeout 属性的任何设置

【问题讨论】:

    标签: c# .net-3.5 transactions strongly-typed-dataset


    【解决方案1】:

    异常可能有各种原因。也许您的实际 SQL 连接超时?当您的表需要重组时可能会发生这种情况(这里只是推测)

    作为一个sidenode,你看过SqlBulkCopy吗? (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx) ->> 让您可以高效地使用其他来源的数据批量加载 SQL Server 表。

    根据我的经验,这非常快。

    【讨论】:

    • 我只是将其拆分为在六个月之间加载,它工作得很好,所以我得到了数据。只是想知道如何处理这么多的数据。如果在事务范围内没有它,它会运行得更快并且不会遇到这种情况,但是如果其中一行遇到了数据集在构建时不知道的某些检查约束,我想停止它。至于批量复制,我还没有看,将来会考虑使用。我插入的表是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多