golang database 全局

golang database 全局,第1张

概述重要的前提条件: 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


重要的前提条件:

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:

package main import ( "database/sql""fmt""github.com/gorilla/mux" _ "github.com/go-sql-driver/MysqL""log""net/http") var db *sql.DB // global variable to share it between main and the http handler func main(){ fmt.Println("starting up" err error db, err = sqlOpen"MysqL""root@unix(/tmp/MysqL.sock)/mydb" // this does not really open a new connectionif!=nil logFatalf"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 := muxNewRouter rHandleFunc"/" HomeHandler httpHandleListenAndServe":8080"nil func w httpResponseWriterhttpRequest msg string@H_404_231@queryRow"SELECT msg FROM hello WHERE page=?""home").Scan(&msgFprintfw"Database Error!"else msg}

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

以上是内存溢出为你收集整理的golang database 全局全部内容,希望文章能够帮你解决golang database 全局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1286008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存