The Connection Pool
There is a basic connection pool in the
database/sql package. There isn’t a
lot of ability to control or inspect it, but here are some things you might find
useful to know:
- Connection pooling means that executing two consecutive statements on a single database might open two connections and execute them separately. It is fairly common for programmers to be confused as to why their code misbehaves. For example,
LOCK TABLESfollowed by an
INSERTcan block because the
INSERTis on a connection that does not hold the table lock.
- Connections are created when needed and there isn’t a free connection in the pool.
- By default, there’s no limit on the number of connections. If you try to do a lot of things at once, you can create an arbitrary number of connections. This can cause the database to return an error such as “too many connections.”
- In Go 1.1 or newer, you can use
db.SetMaxIdleConns(N)to limit the number of idle connections in the pool. This doesn’t limit the pool size, though.
- In Go 1.2.1 or newer, you can use
db.SetMaxOpenConns(N)to limit the number of total open connections to the database. Unfortunately, a deadlock bug (fix) prevents
db.SetMaxOpenConns(N)from safely being used in 1.2.
- Connections are recycled rather fast. Setting a high number of idle connections with
db.SetMaxIdleConns(N)can reduce this churn, and help keep connections around for reuse.
- Keeping a connection idle for a long time can cause problems (like in this issue with MySQL on Microsoft Azure). Try
db.SetMaxIdleConns(0)if you get connection timeouts because a connection is idle for too long.
- You can also specify the maximum amount of time a connection may be reused by setting
db.SetConnMaxLifetime(duration)since reusing long lived connections may cause network issues. This closes the unused connections lazily i.e. closing expired connection may be deferred.