From 91b9c416fb7a98cc8fac91e017968caf7b89510e Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 26 Jun 2024 21:38:48 +0800 Subject: [PATCH] 1 --- mysql.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mysql.go b/mysql.go index 4ba258d..c7a3a8c 100644 --- a/mysql.go +++ b/mysql.go @@ -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 }