Do I need close veriables?

Discussion in 'ASP General' started by Lin Ma, Jun 23, 2005.

  1. Lin Ma

    Lin Ma Guest

    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
     
    Lin Ma, Jun 23, 2005
    #1
    1. Advertising

  2. Lin Ma

    McKirahan Guest

    "Lin Ma" <> wrote in message
    news:...
    > 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.
     
    McKirahan, Jun 23, 2005
    #2
    1. Advertising

  3. 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.
     
    Bob Barrows [MVP], Jun 23, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denon
    Replies:
    1
    Views:
    580
    Saravana [MVP]
    Nov 14, 2003
  2. Daniel Albisser
    Replies:
    1
    Views:
    1,080
    GaryM
    Apr 7, 2004
  3. Paul van Rossem
    Replies:
    0
    Views:
    604
    Paul van Rossem
    Apr 7, 2005
  4. tshad
    Replies:
    1
    Views:
    467
    Scott M.
    Sep 6, 2006
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    870
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page