I’m using cakephp and jquery, and ajax requests to get mysql data when a new company is selected. When the user selects a new company before the old company is finished loading, it aborts the request and makes a new one. The problem is that it leaves the mysql connection open (for a long time, maybe an hour). I’m using the non-persistent version of mysql connect. When a user holds the down-arrow key to scroll through companies in a multiple select, it opens hundreds of connections within a few seconds, and then everything has ‘too many connections’ errors.
Here is my javascript for when the company is changed
if ($.companiesDivXHR) {$.companiesDivXHR.abort(); }
$.companiesDivXHR = $.ajax({
url:"/companies/details",
success:function(html) {
$("companiesDiv").html(html);
}
});
Another example might help: I have a similar situation when I have an ajax request every 10 seconds, and it aborts if not already finished. The when the requested page takes 30 seconds or so to be delivered, it is aborted every time before it can be displayed (since 30>10), and again, a mysql connection is left open. When the ajax requested page is loaded by itself, there are no problems, and the connection closes as it should. So, the effect is much slower, but with 1 user, over the course of 20 minutes, it again causes the ‘too many connections’ error (if max connections is set to 100).
Referencing PHP ignore_user_abort, it looks like the reason connections weren’t closing for me was that ignore_user_abort was set to false, which is the default. When set to true, the PHP script continues executing after either an ajax
.abort(), or when pressing the escape key in the middle of execution.I tested this by going to the page that was being loaded by the ajax request, and pressing f5 followed by escape continuously. Each time I aborted from the browser, another connection was left open (seen using mytop on Ubuntu), and stayed open in ‘sleep’ mode until whatever timeout was reached (somewhere between 10 and 60 minutes maybe? not sure). When I switched
ignore_user_aborton, I did the same thing, but each connection only stayed open for 30 seconds, which is how long the page itself takes to load.