Golang database/sql.DB.SetMaxIdleConns function example

package database/sql

SetMaxIdleConns sets the maximum number of connections in the idle connection pool.

If MaxOpenConns is greater than 0 but less than the new MaxIdleConns then the new MaxIdleConns will be reduced to match the MaxOpenConns limit

If n <= 0, no idle connections are retained.

Golang database/sql.DB.SetMaxIdleConns function usage example

 const concurrencyLevel = 10
 db := initDB(b,
 "DROP TABLE IF EXISTS foo",
 "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
 `INSERT INTO foo VALUES (1, "one")`,
 `INSERT INTO foo VALUES (2, "two")`,
  )
 db.SetMaxIdleConns(concurrencyLevel) // <-- here
 defer db.Close()

Reference :

http://golang.org/pkg/database/sql/#DB.SetMaxIdleConns

Advertisement