Lin Ma wrote:
Hello,
I have a general question. In my asp page, I have DB connection,
Recordset, and some variables like
dim name, conn, rs
set conn = Server.CreateObject("ADODB.Connection")
...
set rs= server.createObject("ADODB.Recordset")
name = rs("username")
I know I need to close the connection like
conn.close
set conn = nothing
Do I need to close the recordset and variables? What is the reason?
Just the recordset. Unless you've disconnected the recordset by setting its
ActiveConnection property to nothing, then the recordset should be closed
before the connection is closed and destroyed, based on the general
principle that child objects should be cleaned up before their parent
objects. If the recordset is in the process of performing some sort of
activity, then the connection may not be able to close, leaving it orphaned
in your server's memory when your page goes out of scope (this is known as a
memory leak). Enough orphans, and the web server can crash.
In general, most variables are cleaned up when they go out of scope. With
ADO objects, however, it is recommended that you take control of the
process:
1. It is a good idea to close connections as soon as you are finished using
them since they consume resources both on the web server and on the database
server - the general principle is to release expensive objects as soon as
possible
2. It helps overall performance of your server to close connections as soon
as possible because doing so maximizes the usage of ADO Session Pooling,
which re-uses connections that are closed and released to the pool upon
closing. Keeping connections open unnecessarily can cause the creation of
more connections than are needed by your application
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.