There is a function to work with the database

db := mysql.New("tcp", "", "127.0.0.1:3306", "root", "", "test") err := db.Connect() if err != nil { log.Println("Не удалось подключиться к базе данных") } rows, _, err := db.Query(Query,args...) if err != nil { log.Println("Не удалось выполнить запрос",Query,err) } return rows 

If there are a large number of requests, (in fact, they can be “clicked” with a mouse, say 10–15 per second), it goes into the handler, “It was not possible to connect and execute the request” and the whole application hangs for about a minute.
After that, for unknown reasons, everything works again.
err wrote read tcp 127.0.0.1:20673->12.0.0.1.13030: wsarecv: An established connection was aborted.
Tell me please, what is the matter and what can I do?

  • the campaign was in defer db.Close () - Rakzin Roman

1 answer 1

Always when working with the database/sql package you need to keep track of open resources. In your case, you left the connection to the database ( db ) and the rows rows unclosed.

sql.Rows keeps the connection from the connection pool open until you walk through them all with the help of rows.Next() or until you manually close the connection with the rows.Close() method.

When you “nakltsali” 10-15 times connections, then, most likely, rested against the web of maximum open connections from the MySQL server. Check its settings.

Apparently, the external code that calls your function is iterated over rows ( rows ) and automatically terminates the connection to the database when the passage ends.

As a solution, I would suggest:

  1. Rewrite the function so that the processing of the rows takes place inside it and return not sql.Rows , but some processed result. Then you can write to the function

defer db.Close() defer rows.Close()

and everything will work fine

  1. In external code, correctly handle the closing of sql.Rows .

Good material on working with the database in Golang can be read here .

  • Yes I understand. There nakosyachil and the connection was not closed. Thank you - Rakzin Roman