【问题标题】:.NET TransactionScope Timeout Issue/ EF.NET TransactionScope 超时问题/ EF
【发布时间】:2011-10-11 13:18:39
【问题描述】:

我有一个 WinForms 项目,我必须在其中读取大量 xml 文件 (2gb+) 并将数据存储在 MSSQL 数据库中。
10 分钟后,我收到错误消息:
"与当前连接关联的事务已完成但尚未被释放。必须释放该事务,该连接才能用于执行SQL语句。"

我认为这一定是超时问题,所以我做了以下操作:

我使用我的代码:

using (tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
{
   IsolationLevel = System.Transactions.IsolationLevel.Serializable,
   Timeout = TimeSpan.Zero //TimeSpan.FromHours(2)
}))
{
    /*Here I read out xml, and use EF to write to DB*/
}

我在 App.config 中添加了:

<system.transactions>
  <defaultSettings timeout="2:00:00"/>
</system.transactions>

我在 machine.config 中添加了:

<system.transactions>
   <machineSettings maxTimeout="2:00:00"/>
</system.transactions>

在 MSSQL 中,我将远程查询超时设置为 0

仍然会发生此超时。我做错了什么?

【问题讨论】:

    标签: .net entity-framework transactions transactionscope


    【解决方案1】:

    我首先要指出的是,在单个事务中从 EF 进行如此大规模的上传是错误的。此类操作应在 EF 之外完成,例如将批量插入临时表并合并到主数据集。

    SSIS(SQL Server 集成服务)也可能出现这种情况。

    EF 不是数据迁移或同步的工具,在这种情况下它的表现是悲惨的。

    【讨论】:

      【解决方案2】:

      在 using 块之外读取 xml 文件。

      我建议直接使用sqlcommand 来插入记录。不要使用 EF,因为将对象转换为记录需要时间。

      另外,我会使用 SQLTransaction 而不是 scope 。虽然,作用域将在下面使用 sqltransaction,但您可以使用 sqltrasnaction 获得更多控制。

      我的代码看起来像

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多