469,645 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Do I need close veriables?

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?

Thanks,

Lin
Jul 22 '05 #1
2 1627
"Lin Ma" <a@a.com> wrote in message
news:ef**************@TK2MSFTNGP09.phx.gbl...
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?

Thanks,

Lin

My habit is to
.Close
if I
.Open
and to
Set {object} = Nothing
if I
Set {object} = CreateObject()

The purpose if to free up resources when they're no longer needed.
Jul 22 '05 #2
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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by JatP | last post: by
2 posts views Thread by kevin dalby | last post: by
6 posts views Thread by mike | last post: by
5 posts views Thread by Eric Layman | last post: by
6 posts views Thread by zaina | last post: by
3 posts views Thread by Eric_Dexter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.