PHP Save connection PDO ATTR_PERSISTENT

I am running endless PHP Script with a persistent PDO connection like this:

$conn=new PDO(
  'mysql:host=127.0.0.1','user','pass', array(PDO::ATTR_PERSISTENT => true)
);

      

The mySQL variable wait_timeout

is equal 28800

and I left the Script idling for 12 hours to test; and the connection is automatically dropped, so I'm assuming the attribute PDO::ATTR_PERSISTENT

is being replaced by a system variable wait_timeout

.

So I was wondering if theres a parameter or other PHP method to keep the connection while PHP Script is running, and if that would be good practice.

My plan B will execute a resourceless mySQL query every 60 minutes until the hours are reset.

OS: 4GB RAM VBS Debian 64-bit SSD

+3


source to share


1 answer


I would suggest you another way to handle situations like this. Whenever you need to run a request in your long running script, make sure the connection exists. 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 a complete class example here . I also suggest that you explicitly close the connection in your script if you know you won't be using it for a long period of time.

+5


source







All Articles