3

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_PERSISTENT => true)
);

The mySQL wait_timeout variable is set to 28800 and I left the script on idle for 12 hours for test purposes; and the connection is dropped automatically so I presume the PDO::ATTR_PERSISTENT attribute is superseded by the system variable wait_timeout.

So I was wondering whether theres a setting or another PHP method to keep the connection breathing as long as the PHP script is running and whether or not that would be a good practice.

My plan B would be executing a resource-less mySQL query every 60 minutes to reset the clock.

OS: 4GB RAM VPS Debian 64 Bit SSD

dynamic
  • 46,985
  • 55
  • 154
  • 231
user3162468
  • 425
  • 6
  • 14

1 Answers1

6

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.

Community
  • 1
  • 1
dynamic
  • 46,985
  • 55
  • 154
  • 231