Will these objects be closed and destroyed?

L

Luis

In the code below I have a Response.End in the middle of a Do loop
that is run if the condition in a nested 'If...' statement is not met.
Will the rs and conn objects be closed and destroyed if the
Response.End is called or will they be left open?

connStr = "PROVIDER=OraOLEDB.Oracle; DATA SOURCE=xyz; etc..."
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connStr

rs = Set Conn.Execute("Select A,B,C from TblTableName Where Blah =
'Blah'")
Do While Not rs.eof
If rs("A") = "Tra la la" Then
'Do something
Else
Response.Write("Some message")
Response.End
End If
Loop

'Do other stuff...

rs.Close
Set rs = nothing
conn.Close
Set conn = nothing
 
J

J. Alan Rueckgauer

The theory is that objects declared and instantiated in a page will be
released by IIS when server processing of the page ends. However, you
should *never* rely on server tear-down because it may not always occur when
you think it should (this is especially true in connection pooling or
ASP.Net environments). Proper practice is to organize your code so that
every exit will always pass through an explicit cleanup routine, and make
sure that you close and release "heavy" objects like recordsets and
connections at the earliest possible opportunity.

An easy solution for the code you posted is to set a boolean variable to
false in the 'else' clause of 'if rs("A")' doesn't meet your criteria, then
frame 'do other stuff' in an "If MyBooleanSaysDoOtherStuff Then...End If".
If the condition is true, you'll execute your 'other stuff' and then fall
into the cleanup code. If it's false, you'll simply fall right into the
cleanup code.

Alan
 

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

Members online

Forum statistics

Threads
473,772
Messages
2,569,592
Members
45,104
Latest member
LesliVqm09
Top