PHP Keeping a PDO ATTR_PERSISTENT connection alive

前端 未结 1 1056
生来不讨喜
生来不讨喜 2021-01-14 08:09

I am running an endless PHP Script with PDO Persistent Connection like this:

$conn=new PDO(
  \'mysql:host=127.0.0.1\',\'user\',\'pass\', array(PDO::ATTR_PER         


        
相关标签:
1条回答
  • 2021-01-14 09:08

    I would suggest you another way to handle such situations. Whenever you need to run a query in your long-lasting script ensure that there is a connnection. Otherwise reconnect.

        try {
            echo "Testing connection...\n";
            $old_errlevel = error_reporting(0);
            self::$pdo->query("SELECT 1");
        } catch (PDOException $e) {
            echo "Connection failed, reinitializing...\n";
            self::init();
        }
    

    You can find the full class example here. I also suggest you to explicitly close the connection in your script when you know you will not use it for a long period.

    0 讨论(0)
提交回复
热议问题