'Issue with connections pool on mysql
I'm having some issues with the api I'm developing: sometimes (yes, not always) when I make a request to the golang server from my angular app, it gaves me this error: "sql: database is closed" when I'm tring to execute "QueryContext", but I figured that it happens more frequently on a func that request a larger data from database (200 record top).
Is there a way to check if the connection is still open\valid? shouldn't golang's connection pool do it automatically? (I have other api more "light" in the same server with the same database and everything work smootly)
Is there any MySql setting I should change?(mysql has defaullt settings)
golang version: 1.16, Mysql 8.0.17
Hre is an example of my code: on package database.go
func OpenConnection() (*sql.DB, error)
{
connection, err = sql.Open("mysql", "root@/my_database")
if err != nil {
log.Println("Error opening the connection with the database")
return nil, err
}
return connection, nil
}
On main.go
func main() {
---
http.HandleFunc("/apicall1", customFunc)
http.HandleFunc("/apicall2", customFunc)
http.HandleFunc("/apicall3", customFunc)
}
func customFunc(w http.ResponseWriter, r *http.Request) {
conn, err := database.OpenConnection()
if err != nil {
//handle error 500 response
}
defer conn.Close()
switch(r.URL.Path) {
case "url1": my_package.Func1(conn)
case "url2": my_package.Func2(conn)
case "url3": my_package.Func3(conn)
...
default: //handle not found response
}
}
Solution 1:[1]
You need to configure your sql.DB for better performance from your pool and to avoid hitting max limit of your db allowed connection, here as an example I have made max 100 connection and idle 25 you can change it as per the load. Also it's a nice idea to ping db after making successful connection, just to ensure connection is success.
func OpenConnection() (*sql.DB, error)
{
connection, err = sql.Open("mysql", "root@/my_database")
if err != nil {
log.Println("Error opening the connection with the database")
return nil, err
}
connection.DB().SetMaxIdleConns(25)
connection.DB().SetMaxOpenConns(100)
dberr = connection.DB().Ping()
if dberr != nil {
log.Println("failed to ping db repository : %s", dberr)
return nil, dberr
}
return connection, nil
}
Reference - https://www.alexedwards.net/blog/configuring-sqldb
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Indrajeet |