I have a query that is sent to MySQL server using the jQuery $.getJSON method. While the server is processing this query, I want to issue a new query which supersedes the previous one and kills the old thread.
I have tried using the following method from this post, as shown below:
var request = $.getJSON(....);
request.abort();
However, it only imp开发者_如何学Pythonlements the abort function at browser level. What I need is to send a kill command to the server, so that it does not continue to query something that is already aborted.
The only way to do that, would be to send a new request that commands the server to explicitly about the other request. But for one, I don't think this is possible from PHP at all. Secondly, if you could, it would mean that you have to tell the client about the MySQL thread, so it can later tell the server which one to kill. It will however be hard to make PHP return this information, if it is actually waiting for that query. It is not only the MySQL process that hangs, but the PHP process too.
MySQLi to the rescue.
If you use MySQLi, you can call mysqli_kill
, which accepts a process id. This is the thread is that you get when connecting to MySQL. Call mysqli_thread_id
to get this id.
Storing the thread id.
If you store this id in the session, you may be able to get that id on your next request and kill it. But I'm afraid the session may not be saved by the previous request (since it is still running), so the thread id may not be stored yet.
If this is indeed the case, you can make the first request store the thread id in memcache or in another table (a memory table will do). Use the session id as a key. Then, in your kill request, you can use the session id to find the thread id and kill the other request.
Not for the first request
This will only pose a problem if it is the very first request that hangs, because in that case you will not have a session yet.
(I'm assuming PHP, might be another server process too. Anyway, it's not JavaScript that's directly connecting to MySQL).
精彩评论