Microsoft KB Archive/191572

= INFO: Connection Pool Management by ADO Objects Called From ASP =

Article ID: 191572

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft ActiveX Data Objects 1.5
 * Microsoft ActiveX Data Objects 2.0
 * Microsoft Active Server Pages 4.0
 * Microsoft Data Access Components 2.5

-



This article was previously published under Q191572



SUMMARY
When using ActiveX Data Objects (ADO) within an Active Server Pages (ASP) page, it is important to know how to utilize connection pooling effectively. When using different combinations of implicit and explicit ADO connections with ADO recordsets and commands instantiated with Server.CreateObject or just CreateObject, it is possible for the connections to not be returned to the connection pool. The table in the "More Information" section depicts possible scenarios and outcomes.

Note Connections that use the Jet OLE DB providers and ODBC drivers are not pooled because those providers and drivers do not support pooling.



MORE INFORMATION
By following a connection type from the left of the grid, and a recordset or command type from the top of the grid, you can find the answers to the following two questions:


 * Is the ADO connection (Conn) returned to the connection pool?
 * What statement returns the connection to the pool?

               |Recordset     |Recordset     |Command      |Command |created      |created       |created      |created |with         |with          |with         |with |Server. |CreateObject |Server. |CreateObject |CreateObject |              |CreateObject | ---  Explicit     |1.Yes         |1.Yes         |1.Yes        |1.Yes Connection  |2.Conn.Close  |2.Conn.Close  |2.Conn.Close |2.Conn.Close created with |             |              |             | Server. |             |              |             |   CreateObject |              |              |             | ---  Explicit     |1.Yes         |1.Yes         |1.Yes        |1.Yes Connection  |2.Conn.Close  |2.Conn.Close  |2.Conn.Close |2.Conn.Close created with |             |              |             | CreateObject |             |              | ---  Implicit     |1.No          |1.Yes         |1.No         |1.No   Connection   |2.N/A         |2.Set         |2.N/A        |2.N/A |             |  Recordset   |             | |             |  = Nothing   |             | ---

Sample Code
Note You must change UID= and PWD= to the correct values before you run these codes. Make sure that UID has the appropriate permissions to perform this operation on the database. Explicit Connection - Server.CreateObject

sConnect="DRIVER={SQL Server};SERVER=ServerName;DATABASE=Pubs;UID= ;PWD= " Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open sConnect

Explicit Connection - CreateObject

sConnect="DRIVER={SQL Server};SERVER=ServerName;DATABASE=Pubs;UID= ;PWD= " Set Conn = CreateObject("ADODB.Connection") Conn.Open sConnect

Implicit Connection with Recordset

sConnect="DRIVER={SQL Server};SERVER=ServerName;DATABASE=Pubs;UID= ;PWD= " Set Rs = CreateObject("ADODB.Recordset") Rs.Open "SELECT * FROM Authors", sConnect

How to Recreate These Tests
  Create an ASP page with one of the following scenarios. Sample ASP page: <%@ LANGUAGE="VBSCRIPT" %>

  >%     dim cn(10) dim cmd(10)

For x = 0 to 10 Set cn(x) = Server.CreateObject("ADODB.Connection") cn(x).Open "DRIVER={SQL Server};SERVER=Ovteam;DATABASE=Pubs;UID= ;PWD= " Set cmd(x) = Server.CreateObject("ADODB.Command") cmd(x).activeconnection = cn(x) cmd(x).commandtext = "SELECT * FROM Authors"

cmd(x).execute

Response.Write "Command executed: " & x & ""

Set cmd(x) = Nothing cn(x).close 'comment this line out to recreate the problem Set cn(x) = Nothing Next %>      In SQL Server, open the Performance Monitor: From the Start menu, select Programs, click Administrative Tools (Common) and then select Performance Monitor. Click + to add a counter. Change the object to SQL Server.</li> Select the User Connections counter and click Add.</li> Click Done.</li> Run the ASP page.</li> Watch the User Connections counter. If the connection is being returned to the pool then it will be reused and you only see the counter go up one through two connections from the starting point. If the connections begin to climb, then they are not being returned to the connection pool to be reused, and new connections are being created for each ADO object.</li></ol>

Conclusion
It is generally not a good practice to implicitly create connections. When a connection is implicitly created you do not have a handle to the connection to call and close the connection. The connection continues to remain open and unused until the connection times out. The default timeout with connection pooling enabled for SQL Server is 60 seconds. When connection pooling is not on, SQL Server drops the connection immediately after the ASP page has been parsed but there are other performance issues to consider when connection pooling is not on.

<div class="references_section">