0

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 implements 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.

Community
  • 1
  • 1
Question Overflow
  • 10,925
  • 18
  • 72
  • 110

1 Answers1

2

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).

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • You mean it is difficult but not impossible or it is totally impossible? – Question Overflow Sep 20 '11 at 08:38
  • There are ways, there always are. :) I've never done this before, but I've expanded my answer to describe what I think would work. – GolezTrol Sep 20 '11 at 08:52
  • There will be several processes running concurrently. How do I get the correct thread id as soon as it is generated? – Question Overflow Sep 20 '11 at 09:11
  • When you call `mysqli_connect`, you'll get a new thread. In that same script, you can call `mysqli_thread_id` to get the thread id. You should store this *before* you run your heavy load query. That way, when you call a second request from the same session, you can find the thread id of the previous request and kill it. – GolezTrol Sep 20 '11 at 09:18
  • I got it! Well answered! Thanks alot ;) – Question Overflow Sep 20 '11 at 09:27