企业库5.0和命令超时
我正在使用以下代码,并想知道如果使用企业库的CreateSprocAccessor我们是否需要设置命令超时,如果不是,那么如何管理超时?
var accessor = _sqlDatabase.CreateSprocAccessor("uspGetxyz", new xyzParameters(_sqlDatabase), MapBuilder.MapAllProperties().Build()); //Execute the accessor to obtain the results var Data = accessor.Execute(); xyzList = Data.ToList();
我无法相信它是错误的企业库团队所做的,他们没有给出任何方法来设置Accessor的命令超时,这是他们的一个已知问题http://entlib.codeplex.com/workitem/28586不能相信它,我已经开发了整个项目,并且刚刚开始知道这是一个已知的问题:-( wtf
我已经开始使用Microsoft Enterprise Library了,在正常情况下,DB操作使用“Database”类提供的方法来满足需要。 在某些情况下,对于长时间运行的查询,开发人员希望设置SqlCommand(或DbCommand)类的CommandTimeout属性。 这将允许查询作为命令超时中设置的值长时间执行。
默认情况下,数据访问应用程序块在方法调用中不支持/采用简单的CommandTimeout参数(网上有许多可用的解决方法示例)。 为了实现相同的最小变化,我添加了一个名为“WithCommandTimeOut”的简单函数,在“Microsoft.Practices.EnterpriseLibrary.Data.Database”类中引用timeOutSecond参数,该类返回“Database”类的相同实例。 请参阅下面的更新代码段以了解代码更改。 希望这能解决超时问题。
//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 ie 30 COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET; }
防爆。 数据库db = EnterpriseLibraryContainer.Current.GetInstance(Of Database)(“SmartSoftware”); db.WithCommandTimeOut(0).ExecuteDataSet(CommandType.Text,query);
您可以在AssignParameters
方法中修改xyzParameters
类中的DbCommand
超时:
public void AssignParameters( System.Data.Common.DbCommand command, object[] parameterValues) { command.CommandTimeout = 0; ... }
我们可以在连接字符串中更新这个,增加Connection Timeout = 1000;
Volla! 我已经对Enterprise库的源代码进行了更改,添加了一个新的“execute”方法,它将在Sproc访问器类中使用timeOut参数,并在我的项目中使用这些二进制文件