zoukankan      html  css  js  c++  java
  • 杀死MySQL的连接

    命令  kill 执行线程号

    C# 执行杀死指定的连接

    1  强制Kill掉

            internal protected void KillConnection(MySqlConnection c)
            {
                int threadId = c.ServerThread;
                MySqlCommand cmd = new MySqlCommand("KILL " + threadId, conn);
                cmd.ExecuteNonQuery();
    
                // the kill flag might need a little prodding to do its thing
                try
                {
                    cmd.CommandText = "SELECT 1";
                    cmd.Connection = c;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception) { }
    
                // now wait till the process dies
                bool processStillAlive = false;
                while (true)
                {
                    MySqlCommand cmdProcess = new MySqlCommand("SHOW PROCESSLIST", conn);
                    MySqlDataReader dr = cmdProcess.ExecuteReader();
                    while (dr.Read())
                    {
                        if (dr.GetInt32(0) == threadId) processStillAlive = true;
                    }
                    dr.Close();
    
                    if (!processStillAlive) break;
                    System.Threading.Thread.Sleep(500);
                }
            }
    
            internal protected void KillPooledConnection(string connStr)
            {
                MySqlConnection c = new MySqlConnection(connStr);
                c.Open();
                KillConnection(c);
            }
    

      

    2  友好的关闭连接

    public void CloseConnection(MySqlConnection conn)
    {
    if (conn != null && conn.State != ConnectionState.Closed)
    {
    try
    {
    conn.Close();
    conn.Dispose();
    }
    catch
    {
    }
    }
    }

  • 相关阅读:
    PL/SQL编程急速上手
    MySQL编程
    T-SQL编程
    SQL入门,就这么简单
    前端工具配置(webpack 4、vue-cli 3)
    Vue-router
    Vue组件应用
    Vue.js应用基础
    Bootstrap应用核心
    一篇文章教会你jQuery应用
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5752247.html
Copyright © 2011-2022 走看看