防止
ADO
連接
SQL Server
時的隱式連接
Report Date : ?? 2002/9 |
Prepared by
:
????
鄭
???????????
昀
|
Article last modified on 2002-9 |
|
The information in this article applies to:
ü
????????
Microsoft SQL Server 2000,7.0
ü
????????
Microsoft ADO 2.5
|
問題陳述
:
數據庫服務器:
Microsoft SQL Server 2000
以及
7.0
;
數據庫服務器補?。?
Microsoft SQL Server 2000 ServicePack1
;
ADO
名稱:
Microsoft Data Access - ActiveX Data Objects 2.5 Type Library
ADO
版本:
2.61.7326.0
?
執行下面的
VB
代碼時,我們的開發人員產生了疑問:
?cnn.Open "Provider=SQLOLEDB.1;
Persist Security Info=False;User ID=sa;
Initial Catalog=freemail;Data Source=svr;ConnectionTimeout=10", "", "", -1
????? ?sql = "select * from users"
?? ?Set rs = cnn.Execute(sql)
???
??
Set rs2 = cnn.Execute(sql)
???
??
Set rs3 = cnn.Execute(sql)
執行這段代碼時,在
SQL Server Profiler
中看到,每個
sql
語句執行之前都會有一個
Audit Login
事件。而
Audit Login
事件的解釋是:“
收集自跟蹤啟動后發生的所有新的連接事件,例如客戶端請求連接到運行
Microsoft? SQL Server?
實例的服務器
”
。也就是說,用
Connection
對象連接
SQL Server
之后,每次執行
sql
語句時仍然會重新建立一次連接,即使用的是同一個
Connection
?!
建立連接的事件探查記錄 ( 按時間順序 ) 為:
?
EventClass
|
Text Data
|
TraceStart
|
?
|
Audit Login
(
第一次連接
)
|
-- network protocol: LPC
set quoted_identifier on
set implicit_transactions off
set cursor_close_on_commit off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set language
簡體中文
set dateformat ymd
set datefirst 7
|
SQL:Stm tStarting
|
Select * from users
|
Audit Login
(
第
2
次連接
)
|
-- network protocol: LPC
set quoted_identifier on
set implicit_transactions off…
略
|
SQL:Stm tStarting
|
Select * from users
|
Audit Login
(
第
3
次連接
)
|
-- network protocol: LPC
set quoted_identifier on
set implicit_transactions off…
略
|
SQL:Stm tStarting
|
Select * from users
|
Audit Logout
|
?
|
Audit Logout
|
?
|
Audit Logout
|
?
|
TraceStop
|
?
|
而如果每句
cnn.Execute
后面加上
rs.close()
,
則每個
execute
之前不會有
Audit Login
事件,而是連續的
3
個
SQL:StmtStarting
事件。
這樣頻繁建立物理連接,是否會影響性能?照例說應該重用同一個連接才對呀?
Cause:
這種情況叫做隱式登錄。
當
set
一個
ADO.Recordset
對象接收
ADO.Connection.Execute
返回的記錄集時,就會出現隱式登錄,再次和數據庫服務器建立一次物理連接,而且這個連接還沒有辦法重用,也不能池化。
這個的原因是:
Because the SQL Server OLE DB provider doesn't permit more than one set of results to be pending on a connection where the results are being returned by means of a forward-only, read-only (default-resultset) cursor, the provider needs to create an additional SQL Server connection to execute a second command on the connection. The provider will only do this implicitly if the Data Source property DBPROP_MULTIPLECONNECTIONS is set to VARIANT_TRUE.
?
可以參考微軟的
KB
文檔:
http://support.microsoft.com/default.aspx?scid=kb;EN-GB;q271128&GSSNB=1
《
PRB: Implicit Connections Created by the SQL Server OLE DB Provider (SQLOLEDB) Are Not Pooled
》
?
【不會重復建立數據庫連接的代碼片斷】:
?
通過改變 ADO.Recordset 的屬性避免隱式登錄 ?
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
cn.open ..........
?
rs.CursorType = adOpenStatic
rs.ActiveConnection = cn
rs.Open "select * from orders"
rs.CursorType = adOpenStatic
rs2.ActiveConnection = cn
rs2.Open "select * from orders"
看來,確實如微軟所說的,只有接收默認的記錄集時才會發生隱式連接。如果設置 ADO.Recordset 為其它類型,如靜態集,就不會發生這個問題。
當然,默認的記錄集的屬性
forward-only
、
read-only
情況執行速度最快。
?
Writen by
zhengyun@tomosoft.com
?
本文檔所包含的信息代表了在發布之日,
ZhengYun
對所討論問題的當前看法,
Zhengyun
不保證所給信息在發布之日以后的準確性。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=12694
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
