alternative for doctrine deprecated ping()

3k views Asked by At

Doctrine\DBAL\Connection::ping()

is marked as @deprecated

I found this commit that introduced it but no info about successor for this method was given.

I would like to know what is expected alternative for this functionality.

Should I just rely on isConnected()?

1

There are 1 answers

0
Gordienko Constantin On BEST ANSWER

I dug a bit deeper, and found this pull request

The author of this change says:

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).

So, calling just a isConnected() is not very reliable, and you need to execute some kind of real query, for example: SELECT 1; Hope this will help you!