Go Database/sql source Code Analysis (c) SQL. DB data structure

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The most core data structure in package SQL is SQL. DB, which provides a unified abstraction for the upper-level application, which does not represent a database connection or a connection pool, which is a comprehensive abstract data structure designed by the author of the SQL package to implement a number of functions, such as concurrent access security control, connection pooling, and so on. See code comments for details

221 Type db struct {#通过driverName获取driver, obtains the original connection to the DB via the driver Open () method, SQL. Open () Creates a DB instance 222 driver driver. DRIVER#DB connection string, creating DB instance does not understand create connection, only use time to create Connection 223 DSN string 224//numclosed is an atomic counter which Represen TS A total number of 225//closed connections. Stmt.openstmt checks it before cleaning closed 226//connections in STMT.CSS. #启动以来关闭的连接数 227 numclosed UInt64 228 229 mu sync.     Mutex//protects following fields #空闲连接池, the connection is immediately placed in the connection pool Freeconn []*driverconn #当db. Numopen >= Db.maxopen, the connection queued for creation is added to the change data structure 231 connrequests []chan connrequest# the number of currently open connections 23 2 Numopen int# The number of connections currently waiting to be created, Len (connrequests) and pendingopens differences are: One is waiting for a queued connection, not yet getting the qualifications to create a connection, one is already getting ready to create 233 PENDINGOP ENS int 234//used to signal the need for new connections 235//a goroutine running Connectionopener () reads on This Chan and 236//MaybeopennewconnectIons sends on the Chan (one send per needed connection) 237//It's closed during db. Close (). The close tells the Connectionopener 238//Goroutine to exit. #等待database start a separate goroutine to create the connection required to connect, send the created connection directly to the Connreque STS Chan or put in an idle connection pool 239 Openerch Chan struct{} #关闭状态 closed bool# dependency maintenance, the user maintains the dependencies of DB and other data structures, due to the existence of mutual references and dependencies between data structures and Reuse, the dependency must be released when the resource is cleared 241 DEP map[finalcloser]depset 242 lastput map[*driverconn]string//StackTrace of LA St Conn ' s put; Debug only# Maximum number of idle connections 243 Maxidle int//zero means Defaultmaxidleconns; Negative means 0# maximum number of connections, 0 means unrestricted 244 maxopen int//<= 0 means unlimited 245}


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.