开发者

How to kill mysql process through C#

开发者 https://www.devze.com 2022-12-31 01:50 出处:网络
I am getting \"too many connections\" problem in an Asp .Net Mvc application which get fix when i manually kill process through Mysql v6.56 IDE, But on remote hosting computer where i can\'t kill proc

I am getting "too many connections" problem in an Asp .Net Mvc application which get fix when i manually kill process through Mysql v6.56 IDE, But on remote hosting computer where i can't kill process each time how can i fix this error.

I have tried making a connection to information_schema DB's 开发者_如何学编程PROCESSLIST table but when connection is about to execute command there comes an error "access denied of root@loclahost information_schema". I also have tried to grant all privileges to root@loclahost but still i am not able to fix this problem. I have been coding the same way from last two years but in this application i am getting this problem i have use close each connection in every method. Please if some one have ever got this problem or know the answer.Please help me. Thanx in advance


You need to close the connections from within your code, by calling close() on your connection Objects. Killing the threads through MySQL server is a horrible idea


' code in vb.net you can convert it to c# easily
Dim ConnectionString As String = "your connection string"
Public Function KillAllMySQL() As Integer
    Dim query As String = "SHOW FULL PROCESSLIST"
    '
    Try
        Dim mySqlConnection As MySqlConnection = New MySqlConnection(ConnectionString)
        ' 2. open connection to database using connectionString
        mySqlConnection.Open()
        ' 3. Create a command to hold values
        Dim objCmd As New MySqlCommand(query, mySqlConnection)
        ' 4. Add parameters for sqlCommand
        mySqlDataReader = objCmd.ExecuteReader()
        If mySqlDataReader.HasRows Then
            Do While mySqlDataReader.Read()
                ' kill processes with elapsed time > 200 seconds and in Sleep 
                If mySqlDataReader.GetInt32(5) > 200 And mySqlDataReader.GetString(4) = "Sleep" Then
                    KillMySqlProcess("KILL " & mySqlDataReader.GetInt32(0))
                End If
            Loop
        End If
        If Not mySqlDataReader Is Nothing Then mySqlDataReader.Close()
        If Not mySqlConnection Is Nothing Then mySqlConnection.Close()
    Catch ex As MySqlException
        Return -1
    End Try
    Return 0
End Function

Public Function KillMySqlProcess(ByVal myQuery As String) As Integer
    '1. Create a query
    Dim query As String = myQuery
    '
    Try
        Dim mySqlConnection As MySqlConnection = New MySqlConnection(ConnectionString)
        ' 2. open connection to database using connectionString
        mySqlConnection.Open()
        ' 3. Create a command to hold values
        Dim objCmd As New MySqlCommand(query, mySqlConnection)
        objCmd.ExecuteNonQuery()
        mySqlConnection.Close()
    Catch ex As MySqlException
        Return -1
    End Try

    Return 0
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消