Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

php - alternative for doctrine deprecated ping()

DoctrineDBALConnection::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()?

question from:https://stackoverflow.com/questions/65843650/alternative-for-doctrine-deprecated-ping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...