PHP PDO查询等待不同MySQL连接上的请求,返回“Mysql Server已经消失”

时间:2022-07-13 15:44:53

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

我有一个网站的问题,在不同的部分使用两种类型的连接连接到MySQL数据库:一些PDO,一些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.

该网站的第一部分是使用非常经典的'mysql_query()'PHP函数请求MySQL。这部分对地理数据进行了一些重要的查询。其中一些请求(已经过优化)需要很长时间。

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

该站点的另一部分是最新的,并通过PDO连接使用Doctrine。

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

问题是,当一个大进程在一个浏览器页面中运行时(可能需要大约1分钟来处理并返回页面),如果用户打开另一个页面,PDO连接处于睡眠模式,并保留整个页面加载。 60秒后(mysql的wait_timeout)连接被终止,PDO得到一个异常“MySQL服务器已经消失”。

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.

奇怪的是,只有经典的mysql_connect()和mysql_query()的其他页面可以并行运行而没有问题,只有PDO查询会阻止并最终死亡。

Any input would be really appreciated.

任何输入都会非常感激。

2 个解决方案

#1


1  

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.

关闭这个问题,它实际上与写入时保持的php会话有关,阻止其他进程运行。 session_write_close()解决了它。

#2


-1  

There are various reasons that a connection gets closed.

连接关闭的原因有很多。

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

参考: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.

我也遇到了使用PDO的类似问题,如果主机管理员睡眠超过一分钟,主机管理员就会终止连接。因此,我想出了自己的类,它将包装PDO类。这将检测连接是否已关闭,并将尝试重新连接查询执行。

Answer Below

回答下面

PDO: MySQL server has gone away

PDO:MySQL服务器已经消失

#1


1  

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.

关闭这个问题,它实际上与写入时保持的php会话有关,阻止其他进程运行。 session_write_close()解决了它。

#2


-1  

There are various reasons that a connection gets closed.

连接关闭的原因有很多。

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

参考: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.

我也遇到了使用PDO的类似问题,如果主机管理员睡眠超过一分钟,主机管理员就会终止连接。因此,我想出了自己的类,它将包装PDO类。这将检测连接是否已关闭,并将尝试重新连接查询执行。

Answer Below

回答下面

PDO: MySQL server has gone away

PDO:MySQL服务器已经消失