Database
Sql. DB connection pool need to know:
- Sql. DB built-in connection pool, when the connection is insufficient, a new connection is created automatically, and the newly created connection is
sql.Open()
constructed using the incoming DSN.
- Sql. DB
Close
will only close connections in the connection pool, and the non-return connection will be closed directly upon return.
- When the connection is returned, it will not be
bad connections
put back into the connection pool and will be closed directly.
- Sql. The operation of DB takes
Query/Exec/Ping/Prepare/Begin
one connection from the connection pool or creates a new connection directly and then operates on the new connection.
- Sql. The DB
Query/Exec/Ping/Prepare/Begin
operation takes bad connections
up to 2 attempts to connect and bad connections
creates a new connection directly if the error is still reported.
- Sql. DB even when using the
USE dbname
switch library, operations such as query and exec are taken from the connection pool (not sure which db the connection is taken out of) or create a connection (even which db or non-DB is based on the DSN).
Query
The occupied connection is Rows.Next()
released after the traversal is complete or Rows.Close()
invoked.
QueryRow
The occupied connection is Row.Scan()
released after the call.
Begin
The connection that TX occupies after a transaction is opened, Tx.Commit()
released after or Tx.Rollback()
.
Exec/Ping/Prepare
It's all done. Direct Release Connection
- Sql. Both Conn and SQL.TX
Query/Exec/Ping/Prepare
perform operations on the current connection and do not acquire new connections.
- The same SQL. Conn/sql. Tx
Query
and Exec
do not one operation is not completed to perform another operation, otherwise prone to error (busy buffer)
Setmaxopenconns ()
Default Unlimited
Sets the maximum number of open connections, which, if reached, blocks the operation until other connections are released.
Setconnmaxlifetime ()
Default is 2
Set the maximum number of idle connections for the connection pool, and the returned connection will be closed directly if it arrives.
Setconnmaxlifetime ()
If it is less than 0, it never expires
The maximum life cycle for idle connections should be less than the time-out for database connections.
When the connection is created or when the connection is pooled, the connection is timed out only in the connection pool.
Conn ()
Remove from the connection pool or create a connection to return. In the returned SQL. On the conn, the Ping,Exec,Query,QueryRow,Begin
operation is on the current connection.
Attention:
Sql. The conn connection closes, blocking the transaction on such connections until Tx.commit () or Tx.rollback () is completed.
Golang SQL. Db