First, a factual answer:
The function is there to test if the connection is still active. The intention is to test a connection that could have been idle for a while, for example one that you just grabbed from a connection pool.
The intention is to minimize the risk of failure during an immediately following database request.
An opinionated answer:
This concept is ill-advised, and your feeling that there is something wrong with it is justified.
There are two problems:
A race condition. Even if it ia not likely, the connection can still go bad between the time you test it and the time you use it.
So if you want to write robust code, you still have to make provisions for connection failure in the cide that uses the database connection (retry, not let the error propagate to the user immediately, ...)
In the likely case that the connection is still fine, you incur the unnecessary cost of a client-server round trip, a cost that you will have to pay for every database request.
Therefore, it is better to follow the principle of “try first, apologize later”: don't uae functions like this. Rather, proceed with the database request you actually intend to do, catch any errors, and if there are errors that indicate a broken connection, re-establish it and retry. Your code will become simpler and more robust.
I wouldn't set any timeouts on database connections at all. Rather, use a connection pool that manages that for you. It will keep some connections around in anticipation of database activity and close extra connections after a certain idle time.