R
R
Roman Rakzin2015-08-08 21:58:27
go
Roman Rakzin, 2015-08-08 21:58:27

Doesn't work f-I connect to the database with frequent calls.?

Good afternoon. There is a function for working with the database

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

In other functions, I form sql and pass it to this executing function.
You may say that this is a clumsy option, but still, if you go this way - I ran into a problem when I started to simulate a large number of requests and call this function - I was given an exception, "Could not complete the request."
As far as I understand, one of the functions of the application called this function (new_query_select) when it worked with the database, because if queries are rare, then everything is fine. As soon as I start sending, say 10-15 requests per second, something happens and after that no other request works and goes into the "Could not complete the request" exception.
Question: What should I do to avoid this glitch? Can it be placed in a goroutine or what can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Shevelev, 2015-08-09
@mantyr

You are making too many connections.
You forget to close the resource obtained via db.Query(), of course, if you are using go-sql-driver/mysql and not something else.
You do not display the contents of err, but there can be a lot of useful things there, for example, the contents of an error ... in which it is written in simple language what exactly is broken.
Don't create a connection per request, create one connection (actually there's a connection pool inside) and just use it everywhere and don't forget to close resources.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question