Asp實現(xiàn)的數(shù)據(jù)庫連接池功能函數(shù)分享
數(shù)據(jù)庫連接是一種關鍵的有限的昂貴的資源,這一點在多用戶的網(wǎng)頁應用程序中體現(xiàn)得尤為突出。對數(shù)據(jù)庫連接的管理能顯著影響到整個應用程序的伸縮性和健壯性,影響到程序的性能指標。數(shù)據(jù)庫連接池正是針對這個問題提出來的。數(shù)據(jù)庫連接池負責分配、管理和釋放數(shù)據(jù)庫連接,它允許應用程序重復使用一個現(xiàn)有的數(shù)據(jù)庫連接,而再不是重新建立一個;釋放空閑時間超過最大空閑時間的數(shù)據(jù)庫連接來避免因為沒有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏。這項技術能明顯提高對數(shù)據(jù)庫操作的性能。
但是這項技術一般在java ,php ,.net 里面運用到,asp很少用到,因為一些企業(yè)網(wǎng)站根本就不需要這樣的技術。
也不是不能使用,下面就是研究出來的asp版本,能夠加快網(wǎng)頁的訪問速度,降低數(shù)據(jù)庫的壓力。
1.數(shù)據(jù)庫連接文件 DbPool.asp
< % Const PoolSize = 10 Const 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 <= lenth rndstr = rndstr & Chr(cint(((120 - 98 + 1) * Rnd )+ 97)) i = i + 1 loop GetRandString = rndstr End Function Function CreateDbConn() Dim DbConn,ConnKey Set DbConn = Server.CreateObject("ADODB.Connection") DbConn.Open Connstr ConnKey = GetRandString(10) DbPool.Add ConnKey,DbConn End Function Function GetDbConn() Dim CurKey,Keys If DbPool.Count > 0 Then Keys = DbPool.Keys ' 獲取鍵名。 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 '如果這個連接已經關閉,將其從池里注銷,再新建一個可用的連接并添加到池里 DbPool.Remove CurKey Call CreateDbConn() '新建一個連接并添加到池里 Set GetDbConn = GetDbConn() Else '否則的話,將其從池里注銷,然后將復制的對象返回 DbPool.Remove CurKey Set GetDbConn = Conn Exit Function End If Else Response.Write "連接池已用完,請重新初始化應用程序" Response.End End if End Function Function FreeDbConn(DbConn) DbPool.Add GetRandString(10),DbConn End Function
2.全局文件 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 '建立指定數(shù)目的數(shù)據(jù)庫連接 CreateDbConn() Next End Sub Sub Application_OnEnd DbPool.RemoveAll End Sub %>
3.測試文件 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 Do 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 />" %>
相關文章
使用ASP實現(xiàn)網(wǎng)站的“目錄樹”管理的代碼
使用ASP實現(xiàn)網(wǎng)站的“目錄樹”管理的代碼...2007-09-09asp 實現(xiàn)對SQL注入危險字符進行重編碼處理的函數(shù)
asp 實現(xiàn)對SQL注入危險字符進行重編碼處理的函數(shù)...2007-08-08ASP動態(tài)級聯(lián)菜單實現(xiàn)代碼
asp級聯(lián)菜單效果代碼2008-04-04ScriptHtml 函數(shù)之過濾html標記的asp代碼
ScriptHtml 函數(shù)之過濾html標記的asp代碼...2007-09-09