Mysql 个连接未与 VB.NET 个应用程序关闭

Mysql connections not closing with VB.NET application

这是我第一次来这里,我要疯了,因为我有这个问题:

我正在开发一个 windows 表单应用程序 VB.NET 使用多种类型的连接(mysql,odbc 和 SQL 服务器)一切正常,直到我得到进入 MySQL.. MySQL 服务器是物理 windows 7 台电脑,我通过 IPSEC VPN 隧道连接到它。

我需要每 x 秒执行 2 个 MySQL 连接,如果我在第一次连接后得到某种类型的结果,那么我将打开第二个,依此类推每 x 秒(这都是写的在我的 timer.tick 事件处理程序中)。 问题是 MySQL 服务器上的某些连接经常在 MySQL 服务器上保持活动状态(已建立),我无法找出原因......代码看起来不错,有打开和关闭方法在正确的时间声明,我也尝试过 Dispose、ClearPool 和 ClearAllPools 方法,但我一直保持这些连接,直到我关闭我的程序或它达到连接限制。

代码如下:

Class 连接:

   Public Sub connMySQL()
    Dim connstring As String
    Try
        If stabilimento = "1PR" Then
            If cesoia = "" Then
                connstring = "server=192.168.123.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
            Else
                connstring = "server=192.168.123.253;userid=xx;password=xx;database=xx;Connect Timeout=30"
            End If

        End If
        If stabilimento = "2PR" Then
            If cesoia = "" Then
                connstring = "server=192.168.1.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
            Else
                connstring = "server=192.168.123.253;userid=root;password=xx;database=xx;Connect Timeout=30"
            End If
        End If
   
        conMySql = New MySqlConnection(connstring)
        If conMySql.State = ConnectionState.Closed Then

            conMySql.Open()
              
        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Class执行迭代的地方:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

   connMySQL()
   comm = New MySqlCommand("SELECT count_1,count_2,start_stop,data_ora,id FROM plc_contatori where plc_nome='" + plc_nome + "' and data_ora > '" + data_ieri.ToString("yyyy/MM/dd") + "' order by data_ora desc limit 1", conMySql)

   dr = comm.ExecuteReader()
   While (dr.Read())
       count_1(0) = dr.GetValue(0)

       start_stop(0) = dr.GetValue(2)
       data_ora(0) = dr.GetValue(3)
       If id <> dr.GetValue(4) And count_2(0) <> dr.GetValue(1) Then
           id = dr.GetValue(4)
           count_2(0) = dr.GetValue(1)
       Else
           Exit Sub
       End If
   End While

   dr.Close()
   dr.Dispose()
   conMySql.Close()
   conMySql.Dispose()
   conMySql.ClearPool(conMySql)
   conMySql.ClearAllPools()

   If Not conMySql Is Nothing Then conMySql = Nothing
   comm.Dispose()

        
   If start_stop(0) = 1 Then
       Exit Sub
   End If
       

   Dim dum_count_2 As Integer = count_2(0) - 1       
   connMySQL()
   comm = New MySqlCommand("select count_1,count_2,start_stop,data_ora from plc_contatori where  plc_nome='" + plc_nome + "' and data_ora > '" + data_ieri.ToString("yyyy/MM/dd") + "'  AND count_2=" + dum_count_2.ToString + " ORDER BY data_ora desc limit 1", conMySql)
   dr = comm.ExecuteReader()
   While (dr.Read())
       count_1(1) = dr.GetValue(0)
       count_2(1) = dr.GetValue(1)
       start_stop(1) = dr.GetValue(2)
       data_ora(1) = dr.GetValue(3)
   End While

   dr.Close()
   dr.Dispose()
   conMySql.Close()
   conMySql.Dispose()
   conMySql.ClearPool(conMySql)
   conMySql.ClearAllPools()
   If Not conMySql Is Nothing Then conMySql = Nothing

   comm.Dispose()


   If count_1(0) = count_1(1) And start_stop(1) <> 1 And count_2(0) <> count_2(1) Then
      'sub that reads some values from an odbc connection
       CheckFermo()

   End If

End Sub

注意,我没有在这部分代码中声明的变量在 public class 形式中声明。

我想知道可能出了什么问题...也许在第一个连接被服务器关闭之前建立了第二个连接?

我将 connMySQL 方法更改为 returns 连接字符串的函数。我已经声明了几个变量,所以代码很有意义。我对数据类型做了几个假设。如果这些值实际存储为字符串,您可能必须将其改回 String 和 VarChar。 (我希望他们不是)

您可以使用单个连接,但所有赋值和比较都将在打开的连接下进行。

Private stabilimento As String
Private cesoia As String
Public Function connMySQL() As String
    Dim connstring As String
    Select Case True
        Case stabilimento = "1PR" And cesoia = ""
            connstring = "server=192.168.123.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
        Case stabilimento = "2PR" And cesoia = ""
            connstring = "server=192.168.1.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
        Case Else
            connstring = "server=192.168.123.253;userid=root;password=xx;database=xx;Connect Timeout=30"
    End Select
    Return connstring
End Function

Private count_1(10) As Integer
Private count_2(10) As Integer
Private start_stop(10) As Integer
Private data_ora(10) As Date
Private id As Integer
Private plc_nome As String
Private data_ieri As Date

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Using dt As New DataTable
        Using cn As New MySqlConnection(connMySQL),
            comm = New MySqlCommand("SELECT count_1,count_2,start_stop,data_ora,id 
                                    FROM plc_contatori 
                                    where plc_nome= @plcNome and data_ora > @dataOra 
                                    order by data_ora desc 
                                    limit 1", cn)
            comm.Parameters.Add("plcNome", MySqlDbType.VarChar).Value = plc_nome
            comm.Parameters.Add("dataOra", MySqlDbType.Date).Value = data_ieri '.ToString("yyyy/MM/dd")
            cn.Open()
            Using dr = comm.ExecuteReader
                dt.Load(dr)
            End Using 'closes and disposes reader
        End Using 'closes and dispose connection and command
        count_1(0) = CInt(dt(0)(0))
        start_stop(0) = CInt(dt(0)(2))
        data_ora(0) = CDate(dt(0)(3))
        If id <> CInt(dt(0)(4)) AndAlso count_2(0) <> CInt(dt(0)(1)) Then
            id = CInt(dt(0)(4))
            count_2(0) = CInt(dt(0)(1))
        Else
            Exit Sub
        End If
    End Using 'disposes DataTable
    If start_stop(0) = 1 Then
        Exit Sub
    End If


    Dim dum_count_2 As Integer = count_2(0) - 1
    Using dt As New DataTable
        Using cn As New MySqlConnection(connMySQL),
            comm As New MySqlCommand("select count_1,count_2,start_stop,data_ora 
                                        from plc_contatori 
                                        where  plc_nome= @plcNome 
                                        and data_ora > @dataOra 
                                        AND count_2= @count2 
                                        ORDER BY data_ora desc 
                                        limit 1", cn)
            comm.Parameters.Add("@plcNome", MySqlDbType.VarChar).Value = plc_nome
            comm.Parameters.Add("@dataOra", MySqlDbType.Date).Value = data_ieri '.ToString("yyyy/MM/dd")
            comm.Parameters.Add("@count2", MySqlDbType.Int32).Value = dum_count_2 '.ToString
            cn.Open()
            Using dr = comm.ExecuteReader()
                dt.Load(dr)
            End Using
        End Using
        count_1(1) = CInt(dt(0)(0))
        count_2(1) = CInt(dt(0)(1))
        start_stop(1) = CInt(dt(0)(2))
        data_ora(1) = CDate(dt(0)(3))
    End Using
    If count_1(0) = count_1(1) AndAlso start_stop(1) <> 1 AndAlso count_2(0) <> count_2(1) Then
        'sub that reads some values from an odbc connection
        CheckFermo()
    End If
End Sub