有时VB.NET MySQL,但并不总是冻结整个应用程序

时间:2015-08-28 10:40:44

标签: mysql database vb.net odbc freeze

我用

  • vb.NET 2013
  • Framework 2.0
  • 来自.Net Framework的ODBC连接
  • ODBC MySQL Connector 3.51

有时候,但并非总是如此,当我查询数据库时程序没有响应而且根本没有错误。

重新启动应用程序并再次执行完全相同的操作后,一切正常。

        Function GetData(ByVal xxx1 As Integer, ByVal xxx2 As DateTime, ByVal xxx3 As DateTime) As DataTable
        Dim dt As DataTable = NewDT.GetData

        Try
            SyncLock Master_Con
                Using cmd As New OdbcCommand(myCmdStr, Master_Con)
                    cmd.CommandTimeout = SQLCmd.intCmdTimeout '3
                    cmd.Parameters.Add("xxx1", OdbcType.Int).Value = xxx1
                    cmd.Parameters.Add("xxx2", OdbcType.NVarChar).Value = xxx2
                    cmd.Parameters.Add("xxx3", OdbcType.NVarChar).Value = xxx3
                    Using dr As OdbcDataReader = cmd.ExecuteReader
                        If dr.HasRows Then
                            Dim rowcount As Integer = 0
                            While dr.Read()
                                dt.Rows.Add()
                                For count As Integer = 0 To (dr.FieldCount - 1)
                                    dt.Rows(rowcount).Item(count) = dr(count)
                                Next
                                rowcount = rowcount + 1
                            End While
                        End If
                    End Using
                End Using
            End SyncLock

        Catch OdbcEx As OdbcException
            MessageBox.Show("ODBC-Fehler bei Funktion 'GetData'. " & vbCrLf & vbCrLf & OdbcEx.Message, "App", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Catch ex As Exception
            MessageBox.Show("Systemfehler bei Funktion 'GetData'. " & vbCrLf & vbCrLf & ex.Message, "App", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

        Debug.WriteLine("done.")
        Return dt

    End Function

我的日志说的是这样的:

GetData (28.08.15 - 28.08.15)... done.
GetData2 (28.08.15)... done.
DelData... done.
GetData (28.08.15 - 28.08.15)... done.
GetData2 (28.08.15)... done.
GetData (27.08.15 - 27.08.15)... done.
GetData2 (27.08.15)... done.
GetData (26.08.15 - 26.08.15)... 

程序在运行cmd.ExecuteReader时一直没响应。 因为我以完全相同的方式运行命令多次,我猜测它与超时或mysql服务器/连接繁忙有关。

但是因为它永远不会引发错误/ odbc错误,它只会偶尔发生,然后我不知道如何解决它。

任何帮助都会很好!

1 个答案:

答案 0 :(得分:0)

The problem was about sometimes getting a deadlock.

It now works as it should after invoking the SQLCommand execution in a its own function.

Private Delegate Function ExecScalarDelegate(ByVal cmd As System.Data.Odbc.OdbcCommand, _ 
ByRef ReturnValue As Object) As Boolean

Private Shared Function ExecScalar(ByVal cmd As System.Data.Odbc.OdbcCommand, _ 
ByRef ReturnValue As Object) As Boolean
    ExecScalar = False

    SyncLock cmd.Connection
        ReturnValue = cmd.ExecuteScalar()
    End SyncLock
    Return True
End Function