• 杀死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
    {
    }
    }
    }

  • 相关阅读:
    JavaScript高度和宽度详解
    VC6程序图标
    VC++中的Dlg,App,Doc,view
    Vista桌面图标无法拖动
    VC2008中IE8脚本错误问题解决
    单文件安装包制作(转)
    AutoResetEvent与ManualResetEvent区别
    纯JavaScript中调用WebServices
    动态加载程序集Assembly.Load
    VC++小知识积累
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5752247.html
Copyright © 2020-2023  润新知