R
R
Roman Rakzin2016-01-05 21:15:10
MySQL
Roman Rakzin, 2016-01-05 21:15:10

golang server not responding with frequent db requests?

There is a function for working 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 the mouse, say at a speed of 10-15 per second) goes to the handler - 'Failed to connect and execute the request' and the whole application hangs for about a minute.
After that, for unknown reasons, everything works again.
error read tcp 127.0.0.1:20673->127.0.0.1:3306: wsarecv: An established connection was aborted by the software in your host machine.
Please tell me what is wrong and what can be done

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2016-01-05
@pav5000

You are connecting to the database on every request, that's really bad.
Connect to the database once when the application starts, and when processing a request, use a ready-made connection.

U
uvelichitel, 2016-01-05
@uvelichitel

Try

db.SetMaxIdleConns(n int)
db.SetMaxOpenConns(n int)

to manipulate the available DB connection pool

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question