2020-10-15

数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出。对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标。数据库连接池正是针对这个问题提出来的。数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。

但是这项技术一般在java ,php ,.net 里面运用到,asp很少用到,因为一些企业网站根本就不需要这样的技术。
也不是不能使用,下面就是研究出来的asp版本,能够加快网页的访问速度,降低数据库的压力。

1.数据库连接文件 DbPool.asp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

< %

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!--#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 />"

  

%>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值