Documentation
¶
Index ¶
- Variables
- func Drivers() []string
- func Register(name string, driver driver.Driver)
- type DB
- func (db *DB) Close() error
- func (db *DB) CloseOnDeadline()
- func (db *DB) DisableBadConnRetry()
- func (db *DB) Do(f Handler) error
- func (db *DB) DoContext(ctx context.Context, f Handler) error
- func (db *DB) Ping() error
- func (db *DB) PingContext(ctx context.Context) error
- func (db *DB) SetConnMaxIdleTime(d time.Duration)
- func (db *DB) SetConnMaxLifetime(d time.Duration)
- func (db *DB) SetMaxIdleConns(n int)
- func (db *DB) SetMaxOpenConns(n int)
- func (db *DB) Stats() DBStats
- type DBStats
- type Handler
Constants ¶
This section is empty.
Variables ¶
var ( ErrDriverClosed = errors.New("pool: driver is closed") ErrDupClose = errors.New("pool: duplicate driverConn close") // ErrConnDone is returned by any operation that is performed on a connection // that has already been returned to the connection pool. ErrConnDone = errors.New("pool: connection is already closed") )
Functions ¶
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a database handle representing a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines.
The sql package creates and frees connections automatically; it also maintains a free pool of idle connections. If the database has a concept of per-connection state, such state can be reliably observed within a transaction (Tx) or connection (Conn). Once DB.Begin is called, the returned Tx is bound to a single connection. Once Commit or Rollback is called on the transaction, that transaction's connection is returned to DB's idle connection pool. The pool size can be controlled with SetMaxIdleConns.
func Open ¶
Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information.
Most users will open a database via a driver-specific connection helper function that returns a *DB. No database drivers are included in the Go standard library. See https://golang.org/s/sqldrivers for a list of third-party drivers.
Open may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping.
The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.
func (*DB) Close ¶
Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish.
It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.
func (*DB) CloseOnDeadline ¶
func (db *DB) CloseOnDeadline()
func (*DB) DisableBadConnRetry ¶
func (db *DB) DisableBadConnRetry()
func (*DB) Ping ¶
Ping verifies a connection to the database is still alive, establishing a connection if necessary.
Ping uses context.Background internally; to specify the context, use PingContext.
func (*DB) PingContext ¶
PingContext verifies a connection to the database is still alive, establishing a connection if necessary.
func (*DB) SetConnMaxIdleTime ¶
SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
Expired connections may be closed lazily before reuse.
If d <= 0, connections are not closed due to a connection's idle time.
func (*DB) SetConnMaxLifetime ¶
SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
Expired connections may be closed lazily before reuse.
If d <= 0, connections are not closed due to a connection's age.
func (*DB) SetMaxIdleConns ¶
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.
The default max idle connections is currently 2. This may change in a future release.
func (*DB) SetMaxOpenConns ¶
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).
type DBStats ¶
type DBStats struct { // pool config MaxOpenCount int // Maximum number of open connections to the database. MaxIdleCount int // zero means defaultMaxIdleConns; negative means 0 MaxLifetime time.Duration // maximum amount of time a connection may be reused MaxIdleTime time.Duration // maximum amount of time a connection may be idle before being closed // Pool Status OpenCount int // The number of established connections both in use and idle. IdleCount int // The number of idle connections. // Counters WaitCount int64 // The total number of connections waited for. WaitDuration time.Duration // The total time blocked waiting for a new connection. MaxIdleClosed int64 // The total number of connections closed due to SetMaxIdleConns. MaxIdleTimeClosed int64 // The total number of connections closed due to SetConnMaxIdleTime. MaxLifetimeClosed int64 // The total number of connections closed due to SetConnMaxLifetime. }
DBStats contains database statistics.