Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does It matter If I Create Create Command Once Or On Each Call 1

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US

I need to code a service program that will perform database commands against an AS400/Iseries throughout the day when various events. Does it matter if I create a new command each time I need to issue a command or should I create the commands once in the OnStart stage of the service program and merely change the parametr values? In other words should the bracketed code below within '*** ONLY IN THE ONSTART??? ***' appear once in the Onstart rather than on each occurrance of the event?.

Code:
' *** ONLY IN THE ONSTART??? *****
 Dim SSLTSTBadDataCommand As New iDB2Command
 SSLTSTBadDataCommand = As400Connection.CreateCommand
 SSLTSTBadDataCommand.CommandType = CommandType.Text
 SSLTSTBadDataCommand.CommandText =
                             "UPDATE SSLTST01P1 SET A1BAD = 'Y' " +
                             "WHERE A1TS05 = @item " +
 SSLTSTBadDataCommand.DeriveParameters()
' *** ONLY IN ONSTART ??? ***** 

  SSLTSTBadDataCommand.Parameters("@item").Value = strKeyItem
  SSLTSTBadDataCommand.ExecuteNonQuery()

Dazed and confused.

Remember.. 'Depression is just anger without enthusiasum'.
 


I would create the command object, but not instantiate it until it is needed, then Dispose it when done.

First off, these two lines are a bit redundant:

Dim SSLTSTBadDataCommand As New iDB2Command 'instantiates a command object
SSLTSTBadDataCommand = As400Connection.CreateCommand 'instantiates another command object. The first one above is now "orphaned" and will stay in memory until the GC cleans it up. Change this to:

Dim SSLTSTBadDataCommand As iDB2Command '"New" keyword removed

Then do the rest when it is needed in the code:

SSLTSTBadDataCommand = As400Connection.CreateCommand
SSLTSTBadDataCommand.CommandType = CommandType.Text
SSLTSTBadDataCommand.CommandText =
"UPDATE SSLTST01P1 SET A1BAD = 'Y' " +
"WHERE A1TS05 = @item " +
SSLTSTBadDataCommand.DeriveParameters()
SSLTSTBadDataCommand.Parameters("@item").Value = strKeyItem
SSLTSTBadDataCommand.ExecuteNonQuery()

Finally, when done with the command object, Dispose it, call GC and then set the command object to Nothing:

SSLTSTBadDataCommand.Dispose()
GC.Collect()
SSLTSTBadDataCommand = Nothing

This keeps your program from having the command in memory all the time.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks. That is a really helpful.

I felt I was being a bit clumsy.


Dazed and confused.

Remember.. 'Depression is just anger without enthusiasum'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top