home navigator pointer resources navigator pointer asp tips navigator pointer close your connections
The definitive ASP tips page
tips menu | printable single page version

Note: this is my old ASP tips page and is no longer supported. See the new Visualize web site..

ASP Coding and Style - Close and Free up Recordsets/Database Connections after use

A classic bottleneck this one on servers, and one which can rapidly eat resources if not handled correctly. Ensure that all recordsets and database connections are closed correctly after use, and objects are set =NOTHING where relevant. Generally, this should occur on the same page as they are created/opened (keep code symmetry), which will make best use of the database connection pooling on the server (if using ODBC, make sure connection pooling is enabled in the ODBC driver manager - if the option is not present, it will be worth your while upgrading MDAC on the server, see Data Access Components/ODBC drivers). Example code structure:
 Dim dbConn, RS, sSQL
 Set oDBConn = Server.CreateObject("ADODB.Connection")
 oDBConn.Open "TestDSN"
 
 sSQL = "SELECT Field1, Field2 " &_
        "FROM MyTable "
 
 set rsTest = dbConn.Execute(sSQL)
 ' .... check for SQL error and process results here
 ' .... etc
 
 rsTest.Close
 oDBConn.Close
 Set rsTest = Nothing     ' Free up memory
 Set oDBConn = Nothing

 

DISCLAIMER: Note these pages are a free resource for anyone wishing to reference them. Although every care is taken to ensure their correctness, the author takes no responsibility for any errors or problems that may occur through their use, or indeed misuse. These pages are copyight of Dave Clarke, Visualize Software Ltd 1997-2000 (all rights reserved).


 


© Copyright Dave Clarke, 1996-2008