I intentionally stopped the database after successfully launching my Go program to test the behavior of sql.DB.Ping().
sql.Open is being called in init()
Much to surprise, sql.DB.Ping() did not return an error when my program called it before calling sql.DB.Query().
Why am I doing this?
Just trying to write robust code. Checking the integrity of the DB connection only at startup may not be sufficient for a long-lived production web application.
Code be found here: https://play.golang.org/p/H5Tj-OEMaO
Thank you.
Note: This is a simple program that I took from an instructor and modified to learn about Ping().
I see the same thing you do, Ping() returns nil once it has connected to the database once, regardless of whether it’s still running. This seems like a bug to me (in lib/pq) as the doc string says // Ping the remote server to make sure it's alive.