Golang: Connection to database idle disconnection problem

Source: Internet
Author: User

Golang is doing database operations, generally we use the Open function to create a database (operation) handle: Func Open (drivername, datasourcename string) (*db, error)     We know that the returned *db is a pool of connections with 0 to many underlying connections that can be safely used by multiple go-threads and will maintain their own idle connections. So generally speaking, the open function is called only once, and it is almost seldom necessary to close the db.     Connection pool can make the connection better control, so simple configuration even do not do what configuration can be used, the fact seems to be so, even on the database is OK. But! You will likely encounter the following error: [MySQL] 2017/01/15 12:23:43 packets.go:124:write TCP 127.0.0.1:45697->127.0.0.1:3306:write:broken Pipe     The reason is probably that your connection pool is not properly configured, or configured to set the wrong value, then the reason for the timeout is the following:    1. The maximum number of connections is greater than the maximum number of connections configured on the database server side, The connection timeout    2 occurs when the redundant connection is used. Network jitter: Commit or maintain large amounts of data each time the connection is committed, and the network instability results in a connection timeout    3. The resources used are not released, we usually open only once, get a * DB, but running out of Stmt,rows or TX is not closed, and draining resources can also cause the connection to time out.     for the 3 issues that appear, you can try the following solutions:    1. For the maximum number of connections, call the function Db.setmaxopenconns () setting value (less than the maximum connection for the database configuration)    2. For maintaining large amounts of data, you can attempt to take a transactional operation, if it fails, rollback. And then try again.     3. Using the defer keyword, the associated close function is executed when the action is enabled and the defer *.close () function is added directly to the next line. If you try the solution above, or if you find an occasional connection timeout error, it is likely that the database itself has a timeout setting for the connection, and if the time-out expires the database will unilaterally disconnect the connection, then the connection in the connection pool can be accessed with an error.Each connection in the connection pool maintains a creation time that is automatically checked when the connection is taken, and you can try to call the DBThe Setconnmaxlifetime () method sets the time-out period for db to be less than the time-out for the database.


Golang: Connection to database idle disconnection problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.