Skip to content

Commit

Permalink
check database connection #51 (#53)
Browse files Browse the repository at this point in the history
Adding a ping when database object is created to fail fast.
  • Loading branch information
milad-rasouli authored Mar 16, 2024
1 parent b9d3e53 commit 79e6602
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 79e6602

Please sign in to comment.