I connect to the database in the init function of a controller, like:
db, err = sql.Open("mysql", "user:pass@tcp(<ip>:3306)/<db>")
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
Then I prepare some statements (db.Prepare
) and finally execute them somewhere in the code, without creating new db connections or anything weird. Just letting go handle the connection pool.
But as you can see in the image, I'm getting a lot of connections and aborted connections which make the server run slow and even crash.
Why is it happening? Also I have around 2000 simultaneous online users which result in about 20 queries per second. I don't think it's much, just in case it mattered.
EDIT: Here's how I run the prepared statements. I have 2 selects, 1 update and 1 insert. Selects are being run like:
err = getStatement.QueryRow(apiKey).Scan(&keyId)
if err != nil {
res, _ := json.Marshal(RespError{"Statement Error"})
w.Write(res)
return
}
Inserts and updates:
insertStatement.Exec(a,b,c)