Cadrach
Cadrach

Reputation: 41

PHP PDO query waiting for a request on a different MySQL connection, returns "Mysql Server Has Gone Away"

I am having a problem with a website, connecting to a MySQL database using two types of connection on different parts: some PDO, some mysql_connect().

The first part of the website is requesting MySQL using the very classic 'mysql_query()' PHP function. This part makes some heavy queries on geographical data. Some of these requests (already optimized) take a long time.

Another part of the site is more recent, and made using Doctrine via a PDO connection.

The problem is, when one of the big processes is being ran in one browser page (can take around 1minute to process and return the page), if a user opens another page the PDO connection is in sleep mode, and holds the whole page from loading. After 60s (wait_timeout of mysql) the connection is killed, and the PDO gets an exception "The MySQL Server has gone away".

What is strange is that other pages with only classical mysql_connect() and mysql_query() can be run without a problem in parallel, only PDO queries are holding back and eventually dying.

Any input would be really appreciated.

Upvotes: 3

Views: 1064

Answers (2)

mysticmo
mysticmo

Reputation: 87

There are various reasons that a connection gets closed.

Reference: https://dev.mysql.com/doc/refman/5.0/en/gone-away.html

I too faced the similar problem on using PDO where the hosting administrator kills the connection if it sleeps more than a minute. Hence I came up with my own class which will wrap the PDO class. This will detect whether a connection is closed and will try to reconnect on query execution.

Answer Below

PDO: MySQL server has gone away

Upvotes: -1

Cadrach
Cadrach

Reputation: 41

Closing this question, it was in fact related to the php session being held up on write, preventing the other process from running. session_write_close() resolved it.

Upvotes: 1

Related Questions