Pooling Connection management by PDO

Asked

Viewed 1,660 times

8

We often see in the applications multithreaded using connection to a database, the use of a Connection Polling, where you keep an open "pool" of connections, thus increasing performance so that you don’t have to keep opening connections at all times or have to wait for another thread finalise a bank operation.

I was seeing some implementations of connection to the database using PHP PDO and all do not care about pooling Connection, and a PHP webservier is accessed simultaneously by hundreds, thousands of connections (multithreaded).

What I see most is something similar to the code below, it’s called the method getConnection() and in the method is created a new instance for the PDO class, that is, each HTTP request is created a new connection without need, and could be applied the use of Connection pooling.

public function getConnection() {
    try {
        // realiza a conexão
        $this->con = new PDO($this->dbType.":host=".$this->host.";dbname=".$this->db, $this->user, $this->senha, 
        array( PDO::ATTR_PERSISTENT => $this->persistent ) );
        // realizado com sucesso, retorna conectado

        return $this->con;
    } catch ( PDOException $ex ){ //caso ocorra um erro, retorna o erro
        echo "Erro: ".$ex->getMessage(); 
    }     
}

Doubts:

The PDO manages this pool of connections automatically?

Every HTTP request is created a connection to the database, made the operation and then closed?

There is no possibility to increase performance in PDO-PHP using a pool of connections? If there is something of the kind, could you leave me links for study, I can’t find complex material that addresses this situation well.

  • I can answer the second question and comment on the third. 2) Yes, a new connection is opened at the beginning of the Request and closed at the end of the Request. 3) Much is said about Long Pooling as a solution to this problem at least on an individual level, per user. I never saw any real demo that wasn’t a chat for technique. In that reply Member @Onosendai cited two other ways (one of them I had never heard of).

  • @Brunoaugusto "Connection pooling" and "long Polling" are two totally different things, for different purposes. Connection pooling is a technique for managing connections between a web server and a database server. Long Polling is a technique for keeping an HTTP connection open between a web server and a client.

1 answer

7

You don’t need to manage this in PHP, because it is used PDO::ATTR_PERSISTENT as true PDO/PHP will reuse connections.

If you use Apache, and configured it to work with 80 threads, for example, you will see that in your database there will be 80 connections and if you follow the life of these connections, you will see that they will last the same time as each thread of the webserver.

The optimization you are looking for is very much linked to the configuration of the webserver. Here comes the experience and the load tests.

  • I never used persistent connections with PDO so, still, it’s persistent per user, sure?

  • @Brunoaugusto, is persistent according to Drive, db host and db user

Browser other questions tagged

You are not signed in. Login or sign up in order to post.