What is the right way or how to close persistent PDO connections in PHP?

Asked

Viewed 92 times

2

I need to close the connection to the Mysql database in PHP in the code below:

try {
  $conn = new PDO('mysql:host=localhost;dbname=banco', "root", "", array(PDO::ATTR_PERSISTENT => true));
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  echo 'ERROR: ' . $e->getMessage();
}
//tenta fechar a conexao
$conn = null;

I need to close the persistent connection to a Mysql database in PHP. I tried to close the connection by setting the variable $conn as null, but it doesn’t seem to be working.

When I use the command SHOW STATUS WHERE variable_name = 'Threads_connected'; in Mysql to check the active connections they do not decrease, but work when the connection is not persistent and Seto the variable $conn as null.

Someone could help me?

  • $conn = null; this is correct, but a persistent connection as well as pools do not close immediately when destroys the object, if you want the connection closing when destroying the object do not use a persistent connection

1 answer

2

Answering your question, according to the documentation here: https://www.php.net/manual/en/pdo.connections.php

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.

That is to say, "Persistent connections are not closed at the end of the script, they are cached to be reused".

When setting the connection object to null ($conn = null) releases the object to be destroyed and the connection to be closed. If you want the connection closed at this point, don’t use a persistent connection, because that’s exactly the opposite of your purpose.

The purpose of a persistent connection is to remain open to avoid the overhead to redo the connection.

  • I appreciate the answer, but then it doesn’t make sense for me to close the persistent connection? However the bank of the project that I am giving maintenance is being overloaded of connections, take me another doubt, if I create 2 persistent connections in a row or more they will be the same? and whether or not they close these connections will change something in both cases?

  • I particularly prefer another resource that is the pooling connections, but that depends on the driver you’re using to connect. The advantage of Polling is that I can set up for example "keep a minimum of 2 open connections, and if necessary open a maximum of 10", but if you prefer to use persistent connections, or even if the driver does not allow it, you can limit the number of connections opened by mysql, in "my.ini". About connections, you should not create multiple, according to the documentation "re-used when Another script requests a Connection using the same credentials" if using the same Credential

Browser other questions tagged

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