Java 如何停止正在运行的 mysql 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2065396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop a running mysql query
提问by slashmais
How do I stop a running mysql query programmatically?
如何以编程方式停止正在运行的 mysql 查询?
The problem I'm facing is that a query is constructed using user supplied data, and may occasionally take a very long time to execute (large tables 15 - 30 million rows). I want to offer the user a cancel-option, but don't know how to stop the currently executing mysql-query.
我面临的问题是查询是使用用户提供的数据构建的,有时可能需要很长时间才能执行(大表 15 - 3000 万行)。我想为用户提供一个取消选项,但不知道如何停止当前正在执行的 mysql-query。
The application is a Java applet-servlet: the user specify criteria in the applet which is passed to the servlet where a handler-class is created to handle the request. This handler-class is self-contained re connect - query - disconnect to mysql.
该应用程序是一个 Java 小程序小服务程序:用户在小程序中指定标准,小程序将传递给小程序,在小程序中创建处理程序类来处理请求。这个处理程序类是独立的重新连接 - 查询 - 与 mysql 断开连接。
Given this scenario, how do I cancel a running mysql-query?
鉴于这种情况,我如何取消正在运行的 mysql-query?
采纳答案by George Johnston
SHOW PROCESSLIST;
KILL <thread_to_be_killed>;
Refer to the documentation for additional details
有关其他详细信息,请参阅文档
回答by ziya
You can issue MySQL specific commands through the standard libraries provided by MySQL and run KILL QUERY. Possibly it's best listing the processes and figuring out which thread you need to kill first.
您可以通过 MySQL 提供的标准库发出 MySQL 特定的命令并运行KILL QUERY。可能最好列出进程并确定您需要先杀死哪个线程。
Depending on your application this might create a security issue though (since you need to give more privileges to the connecting application user).
根据您的应用程序,这可能会产生安全问题(因为您需要为连接的应用程序用户提供更多权限)。
回答by Mark
A separate JDBC connection is required, which means you'll have to create a new database handler instance. Execute SHOW PROCESSLISTstatement and then loop through the results and execute a KILLstatement for each thread id found for the given user.
需要一个单独的 JDBC 连接,这意味着您必须创建一个新的数据库处理程序实例。执行SHOW PROCESSLIST语句,然后遍历结果并为给定用户找到的每个线程 ID执行KILL语句。
The only reliable way I can think to do this is every user would have to log in to the database under a different user name, then get all thread id's for that user and kill all their threads.
我能想到的唯一可靠的方法是每个用户都必须以不同的用户名登录到数据库,然后获取该用户的所有线程 ID 并终止所有线程。