Database connectivity is a critical, limited and expensive resource that is particularly evident in multi-user Web applications. The management of database connections can significantly affect the scalability and robustness of the entire application, affecting the performance metrics of the program. The database connection pool is proposed for this problem. The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection without having to re-establish a database connection that frees up idle time beyond the maximum idle time to avoid missing the database connection caused by not releasing the database connection. This technique can significantly improve the performance of database operations.
But this technology is generally used in Java, PHP,. NET, and ASP is rarely used, because some enterprise Web sites do not need such technology.
It is not impossible to use, the following is the study of the ASP version, can speed up the Web page access speed, reduce the pressure of the database.
1. Database Connection File dbpool.asp
<% Const POOLSIZE = connstr = "Driver={sql Server}"; server= (local); Uid=sa;word=555;database=db "Function getrandstring (lenth) Dim rndstr,i Randomize rndstr =" "i = 1 do while I <= len Th rndstr = rndstr & Chr (CInt (((120-98 + 1) * Rnd) + ()) i = i + 1 loop getrandstring = Rndstr End Function funct Ion Createdbconn () Dim dbconn,connkey Set dbconn = Server.CreateObject ("ADODB. Connection ") dbconn.open connstr Connkey = getrandstring (a) Dbpool.add Connkey,dbconn End Function function Getdbconn
() Dim Curkey,keys If dbpool.count > 0 Then keys = Dbpool.keys ' gets the key name. Curkey = Keys (0) Response.Write "Cur dbconn Key is:" & Curkey & "<br/>" Set Conn = Server.CreateObject ("A Dodb. Connection ") Set Conn = Dbpool (curkey) If conn.state = adstateclosed Then ' If the connection is closed, log it out of the pool, create a new available connection and add it to the pool Dbpool.rem Ove Curkey Call Createdbconn () ' Create a new connection and add it to the pool Set getdbconn = Getdbconn () Else ' Otherwise, log it out of the pool, and then return the replicated object to Dbpool.remove Curke Y Set getdbconn = Conn Exit Function End If Else Response.Write "Connection pool has been used up, please reinitialize the application," Response.End, "" "", "" Freedbconn (dbconn) Dbpool.
Add getrandstring, Dbconn end Function
2. Global file Global.asa
<object id= "Dbpool" progid= "Scripting.Dictionary" scope= "Application" runat= "Server" ></object>
<!--#include file= "dbpool.asp"-->
<%
Sub Application_OnStart
Dim connkey for
i = 1 to Poolsize ' establishes a specified number of database connections
createdbconn ()
Next End
Sub
Sub Application_OnEnd
Dbpool.removeall End
Sub
%>
3. test file test.asp
<!--#include file= "dbpool.asp"-->
<%
Response.Write "Test start:<br>"
Response.Write " Current Objects count: & dbpool.count & <br/>
Set dbconn = Server.CreateObject ("ADODB". Connection ")
Set dbconn = Getdbconn ()
Response.Write" Get one Connection from pool <br/> "
Response.Write "Current Objects Count:" & Dbpool.count & "<br/>"
Set Rs = Server.CreateObject ("ADODB. Recordset ")
rs.open" SELECT * from Mkdb ", dbconn,1,1 does while not
rs.eof
Response.Write Rs (" V_oid ") &" <br/> "
rs.movenext
loop
freedbconn (dbconn)
Response.Write" Free one connection to pool <br/> "
Response.Write" Current Objects count: & dbpool.count & "<br/>"
%>