Do I need close veriables?

L

Lin Ma

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
 
M

McKirahan

Lin Ma said:
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.
 
B

Bob Barrows [MVP]

Lin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

The correct way to close a database connection? 12
Global.asa good practice? 3
ASP Access Recordset Paging 6
Paging 3
paging error handling 1
OLEDB Connection String 2
error handling 4
recordset 5

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top