开发者

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 pa开发者_Python百科ge. 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


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


Generally speaking, manually getting rid of recordsets and other objects is overly cautious, especially at the end of a page.

VBScript has built-in garbage collection and finalizers. As soon as an object is no longer referenced*, it will be GC'd and its class_terminate method called. Thus, if you create a recordset inside a function and do not pass the reference outside the function, then it will be collected and its resources freed at the end of the function when it is no longer referenced. If you place an object in a global variable, then it will still be collected when the script has finished running. You cannot cause a memory leak in this fashion. For this reason, unsetting objects at the end of the script is useless, as they would not survive the end of the script anyway unless you deliberately put them in a longer-lived collection such as Session or Application.

*VBScript's garbage collection guarantees prompt collection, but since it only counts references, it can be fooled by circular references. Keep this in mind and it shouldn't cause you any trouble.

Generally, if there is an object that you need for a short time, referencing it in a function local variable or as the subject of a with block is the best solution. The only reason to manually unset an object is if a) you have a real, demonstrated need to free up resources and either b1) the object in question is referenced in global scope long after it is needed or b2) the object is part of a circular reference.

Now, in your particular case, it looks like the only resource worth worrying about at all is the database connection. If your database is having trouble keeping up with the load, then terminating connections promptly may help. There are two ways to do this. First, manually unset each recordset when you no longer need it, or manually unset each recordset's connection. Second, use the recordsets or connections in a smaller scope so that they will be taken care of automatically. If all you need is the data, for example, then you can use the GetRows method to get the data without the overhead of keeping around the recordset. In my code, I never work with recordsets, commands, or connections directly, but rather use helper functions that take care of the gory ADO details and return the actual data I wanted.


Technically you should destroy objects as soon as possible.

That being said, I do most of my pages as you do and have never had any problems

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜