简体   繁体   English

在 Java 中以编程方式停止执行 Spring JDBC 查询

[英]Stop executing Spring JDBC query programmatically in Java

Currently, I'm running long running MySQL queries using Java Spring JDBC library.目前,我正在使用 Java Spring Z82269B9B71AB4A71432F6958C3 库运行长时间运行 MySQL 查询。 For this program, I want a way of stopping a running query.对于这个程序,我想要一种停止正在运行的查询的方法。 I want to do this programmatically, so killing processes is not what I need here.我想以编程方式执行此操作,因此在这里不需要杀死进程。

Also, setting timeouts and killing threads using Java code is not preferred.此外,不推荐使用 Java 代码设置超时和杀死线程。

Is there a way to do this using programming conventions, or is there any preferred way or design to achieve this use case in Java?有没有办法使用编程约定来做到这一点,或者在 Java 中是否有任何首选的方式或设计来实现这个用例?

You can use the SQL KILL Command to stop a process您可以使用 SQL KILL 命令停止进程

sample样本

MariaDB [(none)]> show processlist;;
+----+-------------+-----------+------+---------+------+--------------------------+-------------------+----------+
| Id | User        | Host      | db   | Command | Time | State                    | Info              | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+-------------------+----------+
|  1 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL              |    0.000 |
|  2 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL              |    0.000 |
|  3 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL              |    0.000 |
|  4 | system user |           | NULL | Daemon  | NULL | InnoDB purge coordinator | NULL              |    0.000 |
|  5 | system user |           | NULL | Daemon  | NULL | InnoDB shutdown handler  | NULL              |    0.000 |
| 46 | root        | localhost | NULL | Query   |   56 | User sleep               | select sleep(199) |    0.000 |
| 47 | root        | localhost | NULL | Query   |    0 | init                     | show processlist  |    0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+-------------------+----------+
7 rows in set (0.00 sec)


MariaDB [(none)]> kill 46;
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> show processlist;;
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| Id | User        | Host      | db   | Command | Time | State                    | Info             | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
|  1 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL             |    0.000 |
|  2 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL             |    0.000 |
|  3 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL             |    0.000 |
|  4 | system user |           | NULL | Daemon  | NULL | InnoDB purge coordinator | NULL             |    0.000 |
|  5 | system user |           | NULL | Daemon  | NULL | InnoDB shutdown handler  | NULL             |    0.000 |
| 47 | root        | localhost | NULL | Query   |    0 | init                     | show processlist |    0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
6 rows in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM