Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to close an Adodb.recordset object before setting it to nothing?

Dim rs as ADODB.Recordset
set rs = ReturnARecordset 'assume ReturnARecordset does just that...

'do something with rs

rs.Close
set rs = Nothing

Is it necessary to call rs.Close before setting it to nothing?

Edit: We have one global connection that we keep open for the duration of the application and all recordset objects use this same connection. I see two answers below talking about the need to close recordsets in order to ensure connections aren't left hanging open. To me that sounds like a lot of silly talk because connections are controlled with connection objects, not recordset objects right? But please let me know if I'm missing something here...

like image 639
Brandon Moore Avatar asked Dec 05 '11 01:12

Brandon Moore


1 Answers

The only reason calling Close explicitly is when you are not sure if the recordset is referenced from somewhere else in your project, usually a result of some sloppy coding.

Dim rs as ADODB.Recordset
Set rs = ReturnARecordset
...
MyControl.ObscureMethod rs
...
Set rs = Nothing

Last line is supposed to terminate the recordset instance without calling Close explicitly, unless MyControl is holding an extra reference and thus preventing normal tear-down. Calling Close on rs will make sure MyControl cannot use its reference for anything useful, crashing in flames in the meantime.

like image 62
wqw Avatar answered Nov 15 '22 16:11

wqw