0

Currently, I'm running long running MySQL queries using Java Spring JDBC library. 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.

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?

5
  • 2
    Similar question, but you should always test the stmt.cancel() with your database and your driver and your use case if it realy works... Commented Mar 18, 2021 at 19:13
  • I'm using Spring JDBC, we have an option to cancel statement there? I can't find it. Commented Mar 18, 2021 at 20:19
  • If you use jdbctemplate you should tag the question accordingly. cancel is a plain JDBC function with which you tagged your question. But it seems you can use it as well in your context. Commented Mar 18, 2021 at 21:34
  • Does this answer your question? Cancel SQL Statement with JDBC Commented Mar 19, 2021 at 1:57
  • @MarmiteBomber I have updated my question, Thanks Commented Mar 19, 2021 at 12:57

1 Answer 1

1

You can use the SQL KILL Command to stop a process

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate your answer, but i want to do this using Java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.