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:
LOCK TABLES
followed by an INSERT
can block because the INSERT
is on a connection that does not hold the table lock.db.SetMaxIdleConns(N)
to limit the number of idle connections in the pool. This doesn’t limit the pool size, though.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.db.SetMaxIdleConns(N)
can reduce this churn, and help keep connections around for reuse.db.SetMaxIdleConns(0)
if you get connection timeouts because a connection is idle for too long.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.Previous: Working with Unknown Columns Next: Surprises, Antipatterns and Limitations