MYSQL- Getting too many aborted connections

Hi Team,

I am using MySQL 5.6. I have one PHP Ajax Script running in loop. Each ajax query creates one DB Connection executes script and closes the connection. (Used PDO for connection)

After the execution of Script, when I checked mysql general log, there is a proper connect and quit.
However, Connection number is not in sequence. Like
Connect 3
3 Quit
Connect 5
5 Quit

When I checked connection status (Show status like ‘%onn%’), I got too many aborted connections.

Could you please help me how to check and resolve the aorted connection issue?

Your help is highly appreciated.

Thanks in advance.

Have you tried having the PHP script create a single connection, then pass that connection to the script that’s being run as a loop?

That is most likely due to AJAX - the first “A” stands for “asynchronous”.

Roughly translated, that means “not in sequential order of time”.

Thanks guys for your reply.

Here is the case
file.php (Opens in browser), user provide details and click on submit. Based on the user innput, file2.php runs through ajax in loop.
In File1.php
UserInput1
Userinput2
for(i=0; i < UserInput1; i++) {
jquery Ajax code and run file2.php also send userinput2
display output from file2.php
}

file2.php
Open DB Connection
do all stuff using connection
display output
Close connection
exit

I’m not 100% clear on the flow logic, but I’m almost 100% certain the efficiency problem is because of using the loop like that to send off requests to the file that returns the database results.

The JSON would be more complex - i.e. a “nested” object - as would the database query and the returned JSON, but I think if you can have the loop build a single JSON object and make a single request it should solve the problems you are having.

How big and complex are the “UserInput” arrays?

Data sent with ajax url is not big max 2 fields.
Field 1 = value1 (number)
Field 2 = value 2 (Number)

For loop can run many times (1 to 100) depending upon the user selection.

Response sent back is not big it is just a success message eg
Ajax request loop starts
first request
response - Files are created successfully for entity 1

second request
response - Files are created successfully for entity 2

third request
response - Files are created successfully for entity 3

and so on.

Hmm. “files created” sounds like there’s more than a series of database queries involved.

Try something like

Client-side:
before the loop initialize an “input” array
inside the loop “push” to the “input” array
after the loop “stringify” the “input” array
send the string in a single request

Server-side
json_decode() the string back into an “input” array
initialize a “result” array
Open a single database connection
run the “input” array through prepared statement
populate the “result” array
json_encode() the “result” array
send it back to client-side for use.

As per the program implemented
myfield1 = 1,2,3,44,55,333,454,… and so on
myfield2 = 1,2,3,4,5

split myfield1 in array

for(i=0; i < count(arr); i++){
datatoajax= {‘field1’:arr[i], ‘field2’ : myfield2}
data = ued_encode(datatoajax)
send jqueryajax request with “data”
}

let me know

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.