Connection pool

A set of open database connections that requests borrow and return. *sql.DB is one: open it once, cap it with SetMaxOpenConns, recycle with SetConnMaxLifetime, and remember that N instances multiply the connections MySQL sees.

Minimal example

go
db.SetMaxOpenConns(20)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(5 * time.Minute)

Learn this concept

Lesson 2.4 · database/sql: pools, contexts and timeouts

Used in exercises

Exercise 2.7 · Seed 50 restaurants and one full menu Exercise 6.12 · Find and fix the first bottleneck
Prerequisite conceptsdatabase/sql