1
This commit is contained in:
parent
8877d9e02a
commit
91b9c416fb
14
mysql.go
14
mysql.go
@ -13,14 +13,18 @@ type Mysql struct {
|
|||||||
passwd string
|
passwd string
|
||||||
database string
|
database string
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
|
maxOpenConns int32
|
||||||
|
maxIdleConns int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Mysql) init(host string, port int32, user string, passwd string, database string) {
|
func (this *Mysql) init(host string, port int32, user string, passwd string, database string, maxOpenConns int32, maxIdleConns int32) {
|
||||||
this.host = host
|
this.host = host
|
||||||
this.port = port
|
this.port = port
|
||||||
this.user = user
|
this.user = user
|
||||||
this.passwd = passwd
|
this.passwd = passwd
|
||||||
this.database = database
|
this.database = database
|
||||||
|
this.maxOpenConns = maxOpenConns
|
||||||
|
this.maxIdleConns = maxIdleConns
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Mysql) Open() error {
|
func (this *Mysql) Open() error {
|
||||||
@ -32,6 +36,10 @@ func (this *Mysql) Open() error {
|
|||||||
this.database)
|
this.database)
|
||||||
db, err := sql.Open("mysql", connStr)
|
db, err := sql.Open("mysql", connStr)
|
||||||
this.db = db
|
this.db = db
|
||||||
|
if err == nil {
|
||||||
|
this.db.SetMaxOpenConns(int(this.maxOpenConns))
|
||||||
|
this.db.SetMaxIdleConns(int(this.maxIdleConns))
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,8 +69,8 @@ func (this *Mysql) Exec(query string, args ...interface{}) (sql.Result, error) {
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMysql(host string, port int32, user string, passwd string, database string) *Mysql {
|
func NewMysql(host string, port int32, user string, passwd string, database string, maxOpenConns int32, maxIdleConns int32) *Mysql {
|
||||||
conn := new(Mysql)
|
conn := new(Mysql)
|
||||||
conn.init(host, port, user, passwd, database)
|
conn.init(host, port, user, passwd, database, maxOpenConns, maxIdleConns)
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user