We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to realize Persistent Database Connections in Phalcon Framework?

How to do it? I'm wondering, is it possible?



9.3k
Accepted
answer

Pass this config to your DB adapter

$config = array(
    "host"     => "localhost",
    "username" => "username",
    "password" => "password",
    "dbname"   => "db_name",
    "persistent" => true
);

For MySQL, you'd need to set it via 'options' PDO array.

   $dbConfig['options'] = [
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_PERSISTENT => 1,
        // Many web applications will benefit from making persistent connections to database servers. Persistent connections are not closed at the end of the script,
        // but are cached and re-used when another script requests a connection using the same credentials.
        // The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application.
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ];


11.1k
Warning: PDO::__construct(): MySQL server has gone away in

It makes me sad and I can't find an unambiguous decision for mysql innodb.

edited May '16

Try to tune up MySQL timeouts, especially wait_timeout

https://www.pythian.com/blog/connection-timeout-parameters-mysql/