Search code examples
databaseasp-classicrecordset

recordset using


Please think a site that has min. 30.000 visitors daily.
The site was coded with asp using sql 2008

There are so many lines and so many database connections

I use recordset like following

set rs=server.createobject.......
set rs2=server.createobject.......
set rs3=server.createobject.......

I put these line at the top of the page. then I use them when I need like;

rs.open "select........."
rs.close

I use them maybe 3 time for each page.

and, at the bottom of the page I write

set rs=nothing
set rs2=nothing
set rs3=nothing

Now my question is that: does it cause memory leak or any other problem?

or should i use like

set rs=server.createobject
rs.open
rs.close
set rs=nothing

I mean should i create object whenever i need and kill it when i finish with it everytime


Solution

  • It's better if you create the recordsets as disconnected from the DB (freeing the connection)

    Dim rs, cmd
    Set rs = server.createobject("ADODB.Recordset")
    Set cmd = server.createobject("ADODB.Command")
    
    ' Init the ADO objects  & the stored proc parameters
    cmd.ActiveConnection = GetConnectionString()
    cmd.CommandText = sqlstmt
    cmd.CommandType = adCmdText
    
    ' Execute the query for readonly
    rs.CursorLocation = adUseClient
    rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
    
    
    ' Disconnect the recordset
    Set cmd.ActiveConnection = Nothing
    Set cmd = Nothing
    Set rs.ActiveConnection = Nothing