Object-lifetime suggestions for longtime running Windows-Service

Topics: Data Access Application Block
Jul 25, 2007 at 6:41 AM
Hi all.
I am implementing an Windows Service thats access (via EntLib DAAB) an MS SQL Server all 15-30seconds. Each access will performe multiple commands. The commands are a set of 5-10 commands, naturally the parameters changes by every run.

What is the best practice for the livetime of the command objects?

A) Every command instantiate new in the functions:
DbCommand com = _db.GetSqlStringCommand(SQL))

B) Create every command at first use and then reuse it?
if (com == null) com = _db.GetSqlStringCommand(SQL));

Is it in case B) necessary to call Database.ClearParameterCache() before parameter-values are set?
Only for the records: the Database-object _db will be created at OnStart() and will be destroyed at OnStop().

Thanks for hints.
Jul 25, 2007 at 1:44 PM
Hi Axel,

Create a new command instance each time and dispose it after you're done with it. DbCommands are stateful, so it's probably not a good idea to keep them around.

Regarding the parameter cache, it is used to cache the discovered parameters, not the values, so it wouldn't be necessary to clear it if you chose case B.