0

我有一个需要从 excel 文件加载数据的 ASP.NET 应用程序。该文件包含大约 20K 条记录。该应用程序从文件中读取数据并遍历每条记录,进行计算和验证,然后将每条记录插入数据库。一切都按预期工作,直到 Insert 方法抛出异常。运行 10 - 11 分钟后抛出错误。注意:所有加载过程都在以下列方式定义的事务范围内运行:

 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))

一直打开 SQLConnection - 我使用 SQL Profiler 确保了这一点。为了使用 DB,我们使用 Microsoft.Practices.EnterpriseLibrary.Data.Database 对象。这是一个插入方法:

public bool InsertInspectionRide(InspectionRideBE be)
    {
        bool result = false;
        try
        {
            using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("InsertInspectionRide",
                be.param1, be.param2))
            {

                cmd.CommandTimeout = 60000000;


                be.IdRide = Convert.ToInt32(db.ExecuteScalar(cmd));

                result = be.IdRide > 0;
            }
        }
        catch (Exception ex)
        {
            if (ExceptionPolicy.HandleException(ex, "DAL"))
            {
                throw;
            }
        }
        return result;
    }

这是一个错误:

    06/28/2016 10:27:14
Type : System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : 
Source : 
Help link : 
Data : System.Collections.ListDictionaryInternal
TargetSite : 
Stack Trace : The stack trace is unavailable.
Additional Info:

MachineName : XXX
TimeStamp : 6/28/2016 7:27:14 AM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
AppDomainName : /XXX-1-131115702788886173
ThreadIdentity : XXX
WindowsIdentity : XXXX
    Inner Exception
    ---------------
    Type : System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    Message : Invalid attempt to call Read when reader is closed.
    Source : System.Data
    Help link : 
    Data : System.Collections.ListDictionaryInternal
    TargetSite : Boolean ReadInternal(Boolean)
    Stack Trace :    at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
       at System.Data.SqlClient.SqlDataReader.Read()
       at System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue)
       at System.Data.SqlClient.SqlCommand.ExecuteScalar()
       at Microsoft.Practices.EnterpriseLibrary.Data.Database.DoExecuteScalar(IDbCommand command)
       at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteScalar(DbCommand command)
       at EnforcementService.DataAccess.InspectionRideDAL.InsertInspectionRide(InspectionRideBE be)

我有关于此错误的搜索信息,主要思想是连接已关闭,但我不知道为什么?

我将不胜感激任何帮助或建议

4

1 回答 1

0

好的,我找到了问题的根源。这是事务超时。System.Transaction 具有 MaxTimeout 属性,默认值为 10 分钟。使用代码或应用程序/网络配置,您只能降低它的价值。为了增加它,您必须在 machine.config 文件中配置它,方法是在文件的配置部分的END中添加以下块。

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

以下是一些相关文章: Override the System.Transactions default timeout of 10 minutes in the code

C# winform 应用程序未获取 Machine.Config 中的 maxTimeout 值

于 2016-07-04T06:56:38.387 回答