This article mainly introduces the sharing of the database connection pool function implemented by Asp. This function can speed up the access speed of web pages and reduce the pressure on the database. Friends who need it can refer to it.
Database connections are a critical, limited, and expensive resource, especially in multi-user web applications. The management of database connections can significantly affect the scalability and robustness of the entire application and affect the performance indicators of the program. The database connection pool was proposed to address this problem. The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of re-establishing one; release database connections whose idle time exceeds the maximum idle time to avoid errors due to failure to release the database connection. Caused by missing database connection. This technology can significantly improve the performance of database operations.
However, this technology is generally used in java, php, and .net. ASP is rarely used because some corporate websites do not need such technology at all.
It’s not that it cannot be used. The following is the asp version that has been researched, which can speed up the access speed of web pages and reduce the pressure on the database.
1. Database connection file DbPool.asp
< %Const PoolSize = 10Const Connstr = Driver={SQL Server};Server=(local);UID=sa;word=555;Database=db Function GetRandString(lenth)Dim rndstr,iRandomizerndstr = i = 1do while i <= lenthrndstr = rndstr & Chr(cint(((120 - 98 + 1) * Rnd )+ 97))i = i + 1loopGetRandString = rndstrEnd Function Function CreateDbConn()Dim DbConn,ConnKeySet DbConn = Server.CreateObject(ADODB.Connection)DbConn. Open ConnstrConnKey = GetRandString(10)DbPool.Add ConnKey,DbConnEnd Function Function GetDbConn()Dim CurKey,KeysIf DbPool.Count > 0 ThenKeys = DbPool.Keys ' Get the key name. CurKey = Keys(0)Response.Write Cur DbConn Key Is : & CurKey & <br />Set Conn = Server.CreateObject(ADODB.Connection)Set Conn = DbPool(CurKey)If Conn.State = adStateClosed Then 'If this connection has been closed, log it out from the pool, create a new available connection and add it to the pool DbPool.Remove CurKeyCall CreateDbConn() 'Create a new connection and add it to the pool Set GetDbConn = GetDbConn()Else 'Otherwise, remove it Log out from the pool, and then return the copied object to DbPool.Remove CurKeySet GetDbConn = ConnExit FunctionEnd IfElseResponse.Write The connection pool has been used up, please reinitialize the application Response.EndEnd ifEnd Function Function FreeDbConn(DbConn)DbPool.Add GetRandString(10) ,DbConnEnd Function
2. Global file global.asa
<object ID=DbPool Progid=Scripting.Dictionary Scope=Application runat=server></object><!--#include file=DbPool.asp-->< %Sub Application_OnStartDim ConnKeyFor i = 1 To PoolSize 'Create the specified number of Database connectionCreateDbConn()NextEnd Sub Sub Application_OnEndDbPool.RemoveAllEnd 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,1Do While Not rs.eofResponse.write Rs(v_oid) & <br />Rs.movenextloop FreeDbConn(dbconn)Response.Write free one connection to pool <br />Response.Write Current Objects count : & DbPool.Count & <br /> %>