We often see in multithreaded applications that use a connection to a database, the use of a connection polling , in which it stores a pool of open connections. way to increase performance so that you do not have to open connections at all times or have to wait for another thread to finish an operation on the database.
I was seeing some connection implementations with the database using the PHP PDO and all are not concerned with connection pooling, and a PHP webserver is accessed simultaneously by hundreds, thousands of multithreaded in>).
What I see most is something similar to the code below, it is called the getConnection()
method and in the method a new instance is created for the PDO class, that is, every HTTP request is created a new connection without necessity, and the use of connection pooling could be applied.
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();
}
}
Questions:
Does the PDO manage this pool of connections automatically?
Does each HTTP request create a connection to the database, made the transaction, and then closed?
Is not it possible to increase performance in PDO-PHP using a pool of connections? If there is anything of the sort, they could leave me links to study, I can not find complex material that addresses this well.