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
    {
    }
    }
    }

  • 相关阅读:
    原型模式
    创造者模式
    抽象工厂模式
    工厂方法模式
    简单工厂模式
    HiveQl 基本查询
    使用VMware安装linux虚拟机以及相关配置
    大数据测试
    使用Pycharm创建一个Django项目
    LoadRunner监控window系统各项指标详解
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5752247.html
Copyright © 2011-2022 走看看