简体   繁体   English

远程数据库连接超时

[英]Remote Database Connection Timeout

I have a vast amount of PHP code which interacts with two databases. 我有大量与两个数据库交互的PHP代码。 It's normal state is to run offline within the local network. 正常状态是在本地网络中脱机运行。 However, when it is connected to the internet, it connects to a remote database to pull and push data. 但是,当它连接到Internet时,它会连接到远程数据库以提取和推送数据。

Unfortunately, when it's not online, the site hangs trying to connect remotely. 不幸的是,当它不在线时,该站点挂起,试图进行远程连接。 I've tried using the checkdnsrr function to see if we are connected to the internet, it's better but still slower than I hope for. 我尝试使用checkdnsrr函数来查看我们是否已连接到Internet,它比我希望的更好,但仍然较慢。

There's far to much to recode here, so anything I can wrap around the PDO database connection statement would be greatly appreciated. 这里有太多要重新编码的内容,因此,我能包装在PDO数据库连接语句中的所有内容将不胜感激。

After a bit more research I found you can set the timeout of the request. 经过更多研究,我发现您可以设置请求的超时时间。 I've set it to 1sec and it seems to work fine. 我将其设置为1sec,它似乎工作正常。

However, I still don't believe it's the best solution, 1 second is still a long time to delay a request for. 但是,我仍然不认为这是最好的解决方案,延迟请求仍然需要1秒的时间。

$DBH = new PDO("mysql:host=$host;dbname=$dbname", $username, $password,array(PDO::ATTR_TIMEOUT => "1"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM