设置PDO连接超时时间小于1秒 [英] Setting PDO connection timeout time less than 1 second

查看:164
本文介绍了设置PDO连接超时时间小于1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP和PDO对象连接到mysql.我的php代码可以连接到3个数据库服务器.如果我尝试连接到数据库服务器#1并且连接失败,我想立即尝试连接到数据库服务器#2.使用以下代码,我可以设置的连接超时时间最低为1秒.

I'm using PHP and a PDO object to connect to mysql. I have 3 DB servers that my php code can connect to. If I try to connect to DB server #1 and the connection fails I would like to immediately try to connect to DB server #2. The lowest I can set the connection timeout time is 1 second with the code below.

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

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

理想情况下,我想将超时时间设置为< 50毫秒.如果可能,则为0ms.有什么办法吗?

Ideally I'd like to set the timeout time to <50 milliseconds. Or 0ms if possible. Is there any way to do this?

推荐答案

这是不可能的,因为底层的MySQL驱动程序不允许:

This is not possible because the underlying MySQL driver won't allow it:

请求#60716 :能够设置PDO连接超时以毫秒为单位

Request #60716: Ability to set PDO connection timeout in milliseconds

这篇关于设置PDO连接超时时间小于1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆