Description
Description
Hello
I just saw that ping is being deprecated (https://www.php.net/manual/en/mysqli.ping.php) in 8.4 because "The reconnect feature has not been available as of PHP 8.2.0, making this function obsolete." - which isn't entirely true.
If you run MySQL + PHP in Roadrunner, FrankenPHP or Swoole, your script will continue to run and your connection will (or can) remain open between requests. The best way to check if the connection is still valid when receiving a request is to ping it, i.e:
if (self::$instance) {
try {
// Attempt to ping static connection.
self::$instance->ping();
// If this works, we can continue with this connection.
return self::$instance;
} catch (mysqli_sql_exception) {
// with MYSQLI_REPORT_STRICT, it throws if it fails
}
}
// If no static var exists or if it was reset; create a new one
self::$instance = self::getInstance(); // this just creates new mysqli of course.
return self::$instance;
I don't care about the reconnection logic, because I am doing that myself, but without ping()
I don't know how to validate the connection.
Is there a better way to handle this scenario? Or does the fact that I use ping
mean I could just as well reconnect every time anyway, from a speed/performance perspective?