sql.DB.Ping() not returning error when DB is down

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.

Show some code.

1 Like

Which SQL driver are you using?

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

SQL Driver being used is:
Postgres (pure Go): https://github.com/lib/pq
Thank you.

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.


Jakob, thank you for taking the time to help me - greatly appreciated!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.