不推荐使用的 ping() 学说的替代方案

alternative for doctrine deprecated ping()

Doctrine\DBAL\Connection::ping()

is marked as @deprecated

我发现 this commit 介绍了它,但没有提供有关此方法的后继者的信息。

我想知道此功能的替代方案是什么。

我应该只依赖isConnected()吗?

我挖得更深一点,发现了这个 pull request

此更改的作者说:

What is the alternative to ensure the connection is not lost due to a connection timeout, so we can safely execute our actual query?

There's no way to ensure that it's not lost. Even if the query you use to probe the connection succeeds, there's no guarantee that the real query will.

Or is the solution to execute a dummy query and catch the ConnectionLost exception?

See the above. The solution is to catch the ConnectionLost exception and handle it depending on the query. E.g. it is safe to re-execute a SELECT but isn't safe to re-execute an INSERT w/o first checking the current state of the database (the connection may fail after the row has been inserted).

因此,仅调用 isConnected() 不是很可靠,您需要执行某种真正的查询,例如:SELECT 1; 希望这对您有所帮助!