开发者

Sql CommandTimeout set for EnterpriseLibrary

I have a sql query which takes longer than 30seconds to execute. I'm aware 开发者_JAVA百科I need to set the CommandTimeout for the command object to overcome this. However, the first place the command object occurs is within the method 'LoadDataSet' within the Enterprise Library.

I don't think I want to be modifying it here.

Could someone please suggest to me an appropriate place to set it?

Thanks!


Try this:

dcCommand = dDatabase.GetSqlStringCommand(sSQLCommand);
dcCommand.CommandTimeout = 60;      //**This is the key statement**
dDatabase.LoadDataSet(dcCommand, dsDataSet , saTableNames);

Instead of this

dDatabase.LoadDataSet(CommandType.Text, sSQLCommand, dsDataSet , saTableNames);


I have started using Microsoft Enterprise Library long back where in normal case the DB operation calls using provided methods of “Database” class fulfill the need. In some case, for the long running query, developer wants to set CommandTimeout property of SqlCommand (or DbCommand) class. This will allow query to be executed long time as value set in command timeout.

By default Data Access Application block does not support/take simple CommandTimeout parameter in method calls (there are many workaround samples available on net). To achieve the same with minimal changes, I have added a simple function named “WithCommandTimeOut” taking timeOutSecond parameter in “Microsoft.Practices.EnterpriseLibrary.Data.Database” class which returns same instance of “Database” class. Refer updated code snippet below for code changes. Hope this will solve timeout Problem.

//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;
}

Ex.

Database db = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("SmartSoftware");
db.WithCommandTimeOut(0).ExecuteDataSet(CommandType.Text, query);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜