golang mysql 全局_golang database 全局

重要的前提条件:

typeDB

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 only be reliably observed within a transaction. 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.

funcOpen

func Open(driverName,dataSourceName string) (*DB,error)

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. Seehttps://golang.org/s/sqldriversfor 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.

##################

Go-MysqL-Driver的作者的回复http://stackoverflow.com/questions/17376207/how-to-share-MysqL-connection-between-http-goroutines

The database/sql package manages the connection pooling automatically for you.

sql.Open(..)returns a handle whichrepresents a connection pool,not a single connection. The database/sql package automatically opens a new connection if all connections in the pool are busy.

Applied to your code this means,that you just need to share the db-handle and use it in the HTTP handlers:

packagemainimport("database/sql""fmt""github.com/gorilla/mux"_"github.com/go-sql-driver/MysqL""log""net/http")vardb*.DB// global variable to share it between main and the HTTP handlerfunc main(){fmt.Println("starting up"err error db,err=Open"root@unix(/tmp/MysqL.sock)/mydb"// this does not really open a new connectionif!=nillogFatalf"Error on initializing database connection: %s"errError())}dbSetMaxIdleConns(100Ping// This DOES open a connection if necessary. This makes sure the database is accessible"Error on opening database connection: %s"r:=muxNewRouterrHandleFunc"/"HomeHandlerhttpHandleListenAndServe":8080"nilfuncw httpResponseWriterhttpRequestmsgstring@H_404_231@QueryRow"SELECT msg FROM hello WHERE page=?""home").Scan(&msgFprintfw"Database Error!"elsemsg}

其余分析:http://studygolang.com/articles/3022

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值