From 79e6602532c58d10c90551abb5a8c5b77a3dfac6 Mon Sep 17 00:00:00 2001 From: Milad Rasouli <106727848+Milad75Rasouli@users.noreply.github.com> Date: Sat, 16 Mar 2024 14:19:00 +0330 Subject: [PATCH] check database connection #51 (#53) Adding a ping when database object is created to fail fast. --- database/database.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/database/database.go b/database/database.go index 79b8ed2..405316b 100644 --- a/database/database.go +++ b/database/database.go @@ -174,13 +174,17 @@ func (db SqlDatabase) AddImage(uuid string, name string, alt string) (err error) } func MakeSqlConnection(user string, password string, address string, port int, database string) (SqlDatabase, error) { - /// Checking the DB connection + /// TODO : let user specify the DB connection_str := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", user, password, address, port, database) db, err := sql.Open("mysql", connection_str) if err != nil { return SqlDatabase{}, err } + + if err := db.Ping(); err != nil { + return SqlDatabase{}, err + } // See "Important settings" section. db.SetConnMaxLifetime(time.Second * 5) db.SetMaxOpenConns(10)