Golang database/sql.DB.SetMaxOpenConns function examples

package database/sql

SetMaxOpenConns sets the maximum number of open connections to the database.

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

If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

Golang database/sql.DB.SetMaxOpenConns function usage examples

Example 1:

 const defaultMaxIdleConns = 2
 // Start by opening defaultMaxIdleConns
 rows := make([]*Rows, defaultMaxIdleConns)
 // We need to SetMaxOpenConns > MaxIdleConns, so the DB can open
 // a new connection and we can fill the idle queue with the released
 // connections.
 db.SetMaxOpenConns(len(rows) + 1) // <-- here

Example 2:

 db.SetMaxIdleConns(1)
 db.SetMaxOpenConns(2)

Reference :

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

Advertisement