Why should I close and destroy a recordset?

人走茶凉 提交于 2019-12-20 04:38:17

问题


I read this article: http://www.utteraccess.com/wiki/Recordsets_for_Beginners, and it says that it's important for me to close and destroy RS's like this:
rs.close Set rs = Nothing
and if I don't, some bugs may happen.

  1. What kind of bugs?
  2. What does rs.close means? Does it mean that the connection to the database is kept open for as long as the rs isn't closed?

回答1:


rs.close cleans up the resources that are used internally, however because ASP is a single process that doesn't have any GC the Set rs = Nothing aids in the clean up.

It de-references your objects, without it you'll have a memory leak. nice isn't it?




回答2:


What T McKeown says is absolutely right

Depending the type of ADODB.Recordset you are returning you can end up with some unnecessary overheads. The purpose of Call rs.Close() is remove these overheads that for most data retrieval purposes are not required.

Take this example, with the connection defined at the start of the page it lives for the entire life of the page.

Dim conn, conn_string, rs

conn_string = "some connection string to your database"

'Memory allocated for ADODB.Connection object
Set conn = Server.CreateObject("ADODB.Connection")
'Connection opened to data source (SQL Server, Oracle, MySQL etc)
Call conn.Open()

Set rs = conn.Execute("SELECT * FROM [sometable]")
Do While Not rs.EOF
  'Long running page (do some processing)
  Call rs.MoveNext()
Loop

'Remove any locks on the tables and dis-associate connection
Call rs.Close()
'Deallocate ADODB.Recordset from memory
Set rs = Nothing

'Connection is still open and needs to be closed
Call conn.Close()
'Connection closed but still allocated in memory
Set conn = Nothing

Over the years I found that working with arrays to display data is far more efficient and saves on the overheads of the ADODB objects.

Dim conn, conn_string, rs, cmd, data

conn_string = "some connection string to your database"

'Memory allocated for ADODB.Command object
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
  'Connection allocated when ADODB.Command is run and only
  'lasts for the life of ADODB.Command object.
  .ActiveConnection = conn_string
  .CommandType = adCmdText
  .CommandText = "SELECT * FROM [sometable]"
  'Allocate memory for ADODB.Recordset
  Set rs = .Execute()
  'Use .GetRows() to build a two dimensional array 
  'containing the ADODB.Recordset data.
  If Not rs.EOF Then data = rs.GetRows()
  Call rs.Close()
  'Deallocate memory for ADODB.Recordset
  Set rs = Nothing
End with
'Deallocate ADODB.Command which closes and de-allocates
'the associated  ADODB.Connection.
Set cmd = Nothing

'All ADODB objects have been deallocated rest of the page can run
'without them using the array to iterate through the data.
If IsArray(data) Then
  'Long running page (do some processing)
End If

For more infomation on working with data in Arrays see this answer to another question, the examples given contain examples of iterating through your data using an Array.



来源:https://stackoverflow.com/questions/22304994/why-should-i-close-and-destroy-a-recordset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!