KILL [CONNECTION | QUERY] processlist_id

KILL允许可选 CONNECTION或QUERY 修饰符:

  • KILL CONNECTION ,KILL与无修饰符相同 :终止与给定关联的连接 processlist_id,在终止该连接正在执行的任何语句之后。
  • KILL QUERY终止连接当前正在执行的语句,但保持连接本身不变。

使用show processlist 查看所有id

Kill 指令使用

如果我们应用执行SQL后,由于锁select for update或者数量太大,导致执行SQL卡在数据库,此时想取消该SQL怎么办?可以通过kill命令停止mysql线程或者是取消该SQL执行,此处需要到底是执行 kill threadId指令还是 kill query theadId指令?

kill与kill query 最大区别是是否取消该连接上执行的所有sql,即是否关闭该线程,如果关闭该线程即对应JDBC中statement关闭

-- 数据库锁住acctno=13记录 然后执行如下更新语句update test set acctname ='12' where acctno=13

如果想取消该SQL执行,可以使用命令 kill query 407 取消SQL执行,执行后407线程并不会消失,如果该连接上有SQL执行会继续执行;但是如果使用kill 407 ,407线程会消失。使用时需要注意二者差异。

线程id除了通过show processlist查看,也可以使用编程的方式获取threadId

 Connection connection = getConnection(); ((MysqlConnection)connection).getSession().getThreadId();
 public void cancel() throws SQLException {    try {      if (this.query.getStatementExecuting().get()) {        if (!this.isClosed && this.connection != null) {          JdbcConnection cancelConn = null;          Object cancelStmt = null;          try {            HostInfo hostInfo = this.session.getHostInfo();            String database = hostInfo.getDatabase();            String user = StringUtils.isNullOrEmpty(hostInfo.getUser()) ? "" : hostInfo.getUser();            String password = StringUtils.isNullOrEmpty(hostInfo.getPassword()) ? "" : hostInfo.getPassword();            NativeSession newSession = new NativeSession(this.session.getHostInfo(), this.session.getPropertySet());            newSession.connect(hostInfo, user, password, database, 30000, new TransactionEventHandler() {              public void transactionCompleted() {              }              public void transactionBegun() {              }            });            //驱动内部使用 KILL QUERY + threadId 指令取消            newSession.sendCommand((new NativeMessageBuilder()).buildComQuery(newSession.getSharedSendPacket(), "KILL QUERY " + this.session.getThreadId()), false, 0);            this.setCancelStatus(CancelStatus.CANCELED_BY_USER);          } catch (IOException var13) {            throw SQLExceptionsMapping.translateException(var13, this.exceptionInterceptor);          } finally {            if (cancelStmt != null) {              ((Statement)cancelStmt).close();            }            if (cancelConn != null) {              ((JdbcConnection)cancelConn).close();            }          }        }      }    } catch (CJException var15) {      throw SQLExceptionsMapping.translateException(var15, this.getExceptionInterceptor());    }  }

是kill 指令,对应的线程ID为407,也有部分工具是发送的kill query指令。

更多相关文章

  1. android EditText使用指南
  2. Android(安卓)Building System 分析
  3. Android编译中m、mm、mmm的区别
  4. 访问Android内部RIL接口(一)
  5. Android(安卓)Accounts Api使用指南
  6. 活用Android的Message Queue(2)
  7. 如何写Android(安卓)init.rc
  8. Android(安卓)Building System 分析
  9. Android(安卓)RoboGuice 使用指南(13):RoboGuice 功能描述

随机推荐

  1. MTK Android Driver :sound
  2. 新添的Activity 要在 manifest 中注册 否
  3. android 设置 源码 修改默认主题为白底黑
  4. [置顶] android 心跳包的分析
  5. Android Permission中英对照
  6. MyEclipse10 配置 Android SDK 时遇到的
  7. [AndroidTips]Declaring a custom androi
  8. 最新eclipse中android插件安装下载地址
  9. Android带返回值的窗口跳转
  10. android edittext 去边框 去下划线