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

Does Phalcon supports persistent DB connection?

Hi. My question is about connection pooling.

Does Phalcon natively supports DB connection pooling? Or: Can I create persistent DB connection like in mysql: mysql_pconect() (or with "p:" prefix in hostname)?



419
Accepted
answer
edited Oct '14

You can make phalcon to create persistent DB connections when setting up the DB service with settings array like this:

app/config/config.ini

[database]
username        = myuser
password        = mypassword
dbname          = mydb
host            = 127.0.0.1
persistent      = true
encoding        = UTF8

public/index.php

  $config = new Phalcon\Config\Adapter\Ini(__DIR__ . '/../app/config/config.ini');

(...)

    //Set the database service
    $di->set('db', function() use ($config){
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host"      => $config->database->host,
            "username"  => $config->database->username,
            "password"  => $config->database->password,
            "dbname"    => $config->database->dbname,
            "charset"   => $config->database->encoding,
            "persistent"=> $config->database->persistent
        ));
    });


36.0k

Thank you! I did not find it in the API docs...



36.0k

Yeah, I need to re-read docs now... I read full docs for Phalcon a year ago, after - only changelogs :)