3

I am using following code and want to know whether we require to set command timeout if using CreateSprocAccessor of enterprise library , if not then how timeout is being managed?

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>();
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
Deepesh
  • 5,346
  • 6
  • 30
  • 45

5 Answers5

3

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);

2

We can update this in the connection string, increase Connection Timeout=1000;

Rollend Xavier
  • 572
  • 4
  • 18
  • This is probably the best answer - you can set it in the connection string, which I have done many times and it works as advertised. However the answer is a bit wrong - it's really Connect Timeout = xxx, not Connection Timeout = xxx. Full example from an application where I use this approach... Data Source=myDbServer;Database=MyDB;Persist Security Info=True;User ID=MyUser;Password=MyPassword;MultipleActiveResultSets=true;Connect Timeout=200;Max Pool Size=200; – Jim Oct 19 '17 at 21:09
2

I can't believe it what blunder enterprise library team has made, they have not given any way to set command time out in case of Accessor, it is a know issue with them http://entlib.codeplex.com/workitem/28586 cant believe it, i have developed whole project and just came to know this a know issue :-(wtf

Deepesh
  • 5,346
  • 6
  • 30
  • 45
1

You can modify DbCommand timeout in your xyzParameters class in AssignParameters method:

public void AssignParameters(
  System.Data.Common.DbCommand command, object[] parameterValues)
  {
    command.CommandTimeout = 0;
    ...
  }
Marijn
  • 10,367
  • 5
  • 59
  • 80
0

Volla!!! i have made changes in source code of Enterprise library, added a new "execute" method which will take timeOut paramerter, in Sproc accessors class and used these binaries in my project

Deepesh
  • 5,346
  • 6
  • 30
  • 45