Golang SQL. Db

Source: Internet
Author: User
Tags dsn prepare

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).
  • QueryThe occupied connection is Rows.Next() released after the traversal is complete or Rows.Close() invoked.
  • QueryRowThe occupied connection is Row.Scan() released after the call.
  • BeginThe connection that TX occupies after a transaction is opened, Tx.Commit() released after or Tx.Rollback() .
  • Exec/Ping/PrepareIt'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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.