如何在运行查询之前检查 PDO MySQL 连接是否有错误?
我的脚本在许多不同的函数中充满了分叉进程。每当调用 pcntl_fork() 时,所有 MySQL 连接都会丢失。如果我在 PDO MySQL 连接上运行查询,则会收到错误“MySQL 服务器已消失”。
问题是,此错误仅在查询执行失败后才会显示在 PDO::errorinfo()
中。我希望能够在尝试运行查询之前检测 MySQL 服务器是否“已消失”。这样我就可以创建一个 PDO 包装器,在这种情况下为我建立新的连接。
有什么想法吗?
My scripts are getting quite riddled with forked processes in a lot of different functions. Whenever pcntl_fork()
is called, all MySQL connections are lost. If I run a query on a PDO MySQL connection I get the error "MySQL server has gone away"
.
The problem is that this error only shows up in PDO::errorinfo()
after a failed query execution. I would like to be able to detect if the MySQL server "has gone away" before I try to run a query. That way I could create a PDO wrapper that makes a new connection for me in such situations.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过示例给您提供两种方法(在某些方面类似):
示例 1:
示例 2:
I give you 2 methods by example (similar in some ways) :
Example 1 :
Example 2 :
仅供参考:这已多次被报告为错误1,2,3 到目前为止尚未修复(5.3.14)。唯一的解决方案是每次分叉子级后都建立一个新连接,并设置 PDO::ATTR_PERSISTENT =>错误的
FYI: this has been reported as a bug several times 1,2,3 with no fix so far (5.3.14). The only solution is to do a new connection each time after forking a child, and also set PDO::ATTR_PERSISTENT => false