【问题标题】:Enterprise Library 5.0 and Command TimeoutEnterprise Library 5.0 和命令超时
【发布时间】:2011-08-27 04:22:03
【问题描述】:

我正在使用以下代码,想知道如果使用企业库的 CreateSprocAccessor 是否需要设置命令超时,如果不需要,那么如何管理超时?

var accessor = _sqlDatabase.CreateSprocAccessor<xyz>("uspGetxyz", 
                     new xyzParameters(_sqlDatabase),
                     MapBuilder<xyz>.MapAllProperties().Build());

//Execute the accessor to obtain the results
var Data = accessor.Execute();
xyzList = Data.ToList<xyz>();

【问题讨论】:

    标签: c# timeout command enterprise


    【解决方案1】:

    我很久以前就开始使用 Microsoft 企业库,在正常情况下,使用“数据库”类提供的方法调用数据库操作可以满足需要。在某些情况下,对于长时间运行的查询,开发人员希望设置 SqlCommand(或 DbCommand)类的 CommandTimeout 属性。这将允许查询在命令超时中设置的值长时间执行。

    默认情况下,数据访问应用程序块不支持/在方法调用中采用简单的 CommandTimeout 参数(网上有许多解决方法示例)。为了以最小的更改实现相同的功能,我在“Microsoft.Practices.EnterpriseLibrary.Data.Database”类中添加了一个名为“WithCommandTimeOut”的简单函数,该函数采用 timeOutSecond 参数,该类返回“Database”类的相同实例。有关代码更改,请参阅下面的更新代码 sn-p。希望这能解决超时问题。

    //Class Level Static Variables
    //Used to reset to default after assigning in "PrepareCommand" static method
    static int DEFAULT_COMMAND_TIMEOUT_RESET = 30;
    
    //Default value when "WithCommandTimeOut" not called
    static int COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET; 
    
    public Database WithCommandTimeOut(int timeOutSeconds)
    {
        COMMAND_TIMEOUT_FOR_THIS_CALL = timeOutSeconds;
        return this;
    }
    
    protected static void PrepareCommand(DbCommand command, DbConnection connection)
    {
        if (command == null) throw new ArgumentNullException("command");
        if (connection == null) throw new ArgumentNullException("connection");
    
        //Here is the magical code ----------------------------
        command.CommandTimeout = COMMAND_TIMEOUT_FOR_THIS_CALL;
        //Here is the magical code ----------------------------
    
        command.Connection = connection;
    
        //Resetting value to default as this is static and subsequent
        //db calls should work with default timeout i.e. 30
        COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET;
    }
    

    例如。 数据库 db = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("SmartSoftware"); db.WithCommandTimeOut(0).ExecuteDataSet(CommandType.Text, query);

    【讨论】:

      【解决方案2】:

      我不敢相信企业图书馆团队犯了什么错误,他们没有给出任何方法来设置命令超时以防 Accessor,这是他们的一个已知问题 http://entlib.codeplex.com/workitem/28586 不敢相信,我已经开发了整个项目,只是知道这是一个已知问题:-(wtf

      【讨论】:

        【解决方案3】:

        我们可以在连接字符串中更新这个,增加Connection Timeout=1000;

        【讨论】:

        • 这可能是最好的答案——你可以在连接字符串中设置它,我已经做过很多次了,它的工作原理和宣传的一样。但是答案有点错误——实际上是 Connect Timeout = xxx,而不是 Connection Timeout = xxx。我使用这种方法的应用程序的完整示例... Data Source=myDbServer;Database=MyDB;Persist Security Info=True;User ID=MyUser;Password=MyPassword;MultipleActiveResultSets=true;Connect Timeout=200;Max Pool Size= 200;
        【解决方案4】:

        您可以在xyzParameters 类中的AssignParameters 方法中修改DbCommand 超时:

        public void AssignParameters(
          System.Data.Common.DbCommand command, object[] parameterValues)
          {
            command.CommandTimeout = 0;
            ...
          }
        

        【讨论】:

          【解决方案5】:

          沃拉!!!我对 Enterprise 库的源代码进行了更改,在 Sproc 访问器类中添加了一个新的“执行”方法,该方法将使用 timeOut 参数,并在我的项目中使用了这些二进制文件

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-04-04
            • 2013-06-20
            • 1970-01-01
            • 1970-01-01
            • 2011-09-19
            • 2012-01-21
            • 1970-01-01
            相关资源
            最近更新 更多