Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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.

@H_197_64@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 StringQueryRow"SELECT msg FROM Hello WHERE page=?""home").Scan(&@H_992_76@msgFprintfw"Database Error!"else msg}

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。