Asp实现的数据库连接池功能函数分享

来源:爱站网时间:2020-03-15编辑:网友分享
数据库连接的资源是很昂贵的,然而大多数Asp数据都是使用连接池进行连接的,下面是爱站技术频道小编带给大家的Asp实现的数据库连接池功能函数分享,一起跟着小编来学习下文吧!

数据库连接的资源是很昂贵的,然而大多数Asp数据都是使用连接池进行连接的,下面是爱站技术频道小编带给大家的Asp实现的数据库连接池功能函数分享,一起跟着小编来学习下文吧!

1.数据库连接文件 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 '建立指定数目的数据库连接
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实现的数据库连接池功能函数分享,都了解的差不多了,总之,大家喜欢爱站技术频道小编的内容,请继续关注js.aizhan.com吧!

上一篇:ASP获取数据库表名、库名、字段名的方法

下一篇:asp正则替换链接实现伪静态效果

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载