ASP resource question
Even though I've been using ASP for nearly 5 years, I have managed to come up with a question that I cannot figure out the answer to myself, since I can see it going both ways.
Back in 2000 (wow, does that seem a generation away!) I learned of the wonderful concept called "include files". And as soon as I figured out that they could encapsulate code and layout, I became a religious user of at least one per project.
Within that include file, I would have an Initialize sub, which would be executed before anything else. Contained within the Initialize sub would be a connection object to the database for the site (assuming it had one), as well as a recordset object to be able to work with it.
When the page was successfully loaded (no errors, etc.) the last line of my PageFooter (layout) sub would be a call to a sub called ClearObjects, which would...you guessed it, destroy the objects.
Back when I started this coding methodology, I was unaware of things such as GetRows() and arrays and wonderful things like that, so I would need the connection and recordset objects open from beginning to end, just because I wasn't sure when I was going to need them.
Now that I'm older and wiser, however, the thought has occurred to me that I can code in such a way as to use my include files to create objects only when I need them and destroy them otherwise.
For example, when querying a database, I could create a sub that would create the database and recordset objects (or call another sub to do the same thing), query the database, retrieve the results, and destroy the object all in one shot. This is as opposed to opening up the objects, waiting to use them, and then killing them at the very end.
This would prove advantageous from a server resource standpoint because, if I made an error outside of my data retrieval stage, then the objects would not exist and therefore not remain in memory.
The problem would potentially occur if a database was to be queried multiple times from within a page.
Let's say for example that I query a database six times within a page, for whatever reason I have. Would creating and destroying the connection and recordset objects six times be more efficient, even though the server has to work to create the objects, or would it be more efficient just to open it once and shut it down at the end?
|