This commit is contained in:
aozhiwei 2024-06-26 21:38:48 +08:00
parent 8877d9e02a
commit 91b9c416fb

View File

@ -13,14 +13,18 @@ type Mysql struct {
passwd string
database string
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.port = port
this.user = user
this.passwd = passwd
this.database = database
this.maxOpenConns = maxOpenConns
this.maxIdleConns = maxIdleConns
}
func (this *Mysql) Open() error {
@ -32,6 +36,10 @@ func (this *Mysql) Open() error {
this.database)
db, err := sql.Open("mysql", connStr)
this.db = db
if err == nil {
this.db.SetMaxOpenConns(int(this.maxOpenConns))
this.db.SetMaxIdleConns(int(this.maxIdleConns))
}
return err
}
@ -61,8 +69,8 @@ func (this *Mysql) Exec(query string, args ...interface{}) (sql.Result, error) {
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.init(host, port, user, passwd, database)
conn.init(host, port, user, passwd, database, maxOpenConns, maxIdleConns)
return conn
}