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).
– Bruno Augusto
@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.
– bfavaretto