2017-10-09 113 views
0

這可能是我遇到過的一個bug最困惑的地方。ADO記錄集在16個創建之後開始建立新連接

下面是一些會產生錯誤的VB代碼。我不關心這個事實,我不必創建新的記錄集,這是故意觸發該錯誤的。

Public Module ConnectionSetup 

Public ActiveTDConnection As ADODB.Connection 

Public Sub ConnectToTD(ByVal userName As String, ByVal Password As String, ByVal dsn As String) 

    ActiveTDConnection = New ADODB.Connection 

    ActiveTDConnection.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=True;" & _ 

       "User ID=" & userName & ";Password=" & Password & ";" & _ 

       "Data Source=" & dsn & ";Mode=Read;" & _ 

       "UID=" & userName & ";AUTHENTICATION=LDAP;Initial Catalog=(Default)" 

    ActiveTDConnection.CommandTimeout = 0 

    ActiveTDConnection.Open(ActiveTDConnection.ConnectionString) 

End Sub 

End Module 

Module Module1 

Sub Main() 

    ConnectionSetup.ConnectToTD("XXX", "XXX", "XXX") 


    Dim DB_qry As String 

    Dim sessionQry As String 

    Dim i As Integer = 1 

    Dim sessionRS As New ADODB.Recordset 


    DB_qry = "SELECT TOP 1 * FROM dbc.databases" 

    sessionQry = "SELECT COUNT(*) FROM dbc.SessionInfo WHERE userName = 'MyUserName'" 


    Do While i <= 18 

     Dim DB_rs As New ADODB.Recordset 

     DB_rs.Open(DB_qry, ActiveTDConnection) 

     sessionRS.Open(sessionQry, ActiveTDConnection) 

     ' This starts going up by 1 on each loop, after 16. 
     Debug.Print(sessionRS.Fields.Item(0).Value) 

     Do While Not DB_rs.EOF 

      MsgBox(i) 

      i += 1 

      DB_rs.MoveNext() 

     Loop 

     DB_rs.Close() 

     DB_rs = Nothing 

     sessionRS.Close() 

    Loop 

End Sub 

第16次迭代後,每個新打開的recordSet都會產生一個新的連接。所以我的代碼隨機開始冒着最大化會話限制的風險。

1.)爲什麼會發生這種情況?

2.)我該如何預防它?

+0

沒有遇到您的錯誤。你可以嘗試刪除對sessionRS的引用 - id est - 在連接(DB_rs)內一次打開一個記錄集?什麼是您的TD客戶端版本? –

+0

我知道這是一個愚蠢的寫法,但這是最簡單的情況下,將觸發我的問題。我使用14.10.05.10 –

+0

你可以運行它爲:DB_qry =「SELECT SESSION」 - 而不是MsgBox(i) - 輸出 - MsgBox(DB_qry.Fields.Item(0).Value) - 看看你是否實際使用不同的連接? –

回答

0
Sub Main() 

    ConnectionSetup.ConnectToTD("XXX", "XXX", "XXX") 


    Dim DB_qry As String 
    Dim sessionQry As String 

    Dim i As Integer = 1 

    Dim sessionRS As New ADODB.Recordset 

    DB_qry = "SELECT SESSION" 

    sessionQry = "SELECT COUNT(*) FROM dbc.SessionInfo WHERE userName = 'MyUserName'" 


    Do While i <= 18 

     Dim DB_rs As New ADODB.Recordset 

     DB_rs.Open(DB_qry, ActiveTDConnection) 

     sessionRS.Open(sessionQry, ActiveTDConnection) 

     ' This starts going up by 1 on each loop, after 16. 
     'Debug.Print(sessionRS.Fields.Item(0).Value) 

     Do While Not DB_rs.EOF 

      MsgBox("Sessions: " & sessionRS.Fields.Item(0).Value & " Cnt: " & i & " SID: " & DB_rs.Fields.Item(0).Value) 

      i += 1 

      DB_rs.MoveNext() 

     Loop 

     DB_rs.Close() 
     DB_rs = Nothing 
     sessionRS.Close() 

    Loop 

End Sub 
相關問題