doulu4233 2017-12-09 15:35
浏览 53


I have a question about a connection to a second host if the first fail down or connection limit reached.

My Host is OVH in France on a shared host (I can't modify apache/php/sql config) and the MAX USER_CONNECTIONS (sql) is 30.

I wanted to switch Host is the first one reach the limit and refuse connection.

Can I use ?:

$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'dbuser';
$dbpswd = 'dbpass';
    $db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname,$dbuser,$dbpswd,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}catch(PDOexception $e){
    $dbhost2 = 'secondhost';
    $dbname2 = 'dbname';
    $dbuser2 = 'dbuser';
    $dbpswd2 = 'dbpass';
        $db = new PDO('mysql:host='.$dbhost2.';dbname='.$dbname2,$dbuser2,$dbpswd2,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    }catch(PDOexception $e){
        die("DB Error please contact the web admin.");

Or if you have a better option..

NB: I do not have much experience in programming, I do not use OOP yet.

  • 写回答

1条回答 默认 最新

  • douzi8127 2017-12-09 16:17

    The idea is right, but this particular implementation is inflexible and unhelpful towards the site admin who is supposed to fix errors.

    Imagine a case when a password has been changed for the main server. Your code will start connecting to the backup server 100% of time, thanks to the unconditional algorithm, and nobody would have an idea on that.

    Given you want to overcome a particular error ("too many connections"), in your catch block you should test whether it is the error you are expecting to, and at least log the error message otherwise. So the site ad min will notice a problem and will have an idea what's going on.

    die("DB Error please contact the web admin."); won't be of a much use for the site admin either, as he won't have an idea what happened with the backup server in turn.

    So for the second one do not catch its exception right in place, but follow the common error handling routine for your site. The easiest one would be a global try catch around the whole code, which looks rather blunt but at least better than several dozens local try catches with its own error message each

    So it should be like

    $dboptions = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
        $db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8mb4",$dbuser,$dbpswd,$dboptions);
    }catch(PDOexception $e){
        if ($e->getMessage() != "whatever particular error message you get") {
        $dbhost2 = 'secondhost';
        $dbname2 = 'dbname';
        $dbuser2 = 'dbuser';
        $dbpswd2 = 'dbpass';
        $db = new PDO("mysql:host=$dbhost2;dbname=$dbname;charset=utf8mb4",$dbuser,$dbpswd,$dboptions);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?