I'm writing a small, simple web app in go using the gorm ORM.
Since the database can fail independently of the web application, I'd like to be able to identify errors that correspond to this case so that I can reconnect to my database without restarting the web application.
Motivating example:
Consider the following code:
var mrs MyRowStruct
db := myDB.Model(MyRowStruct{}).Where("column_name = ?", value).First(&mrs)
return &mrs, db.Error
In the event that
db.Error != nil
, how can I programmatically determine if the error stems from a database connection problem?From my reading, I understand that
gorm.DB
does not represent a connection, so do I even have to worry about reconnecting or re-issuing a call togorm.Open
if a database connection fails?Are there any common patterns for handling database failures in Go?