mysql 增加一个连接池设置

This commit is contained in:
huanghua_2017
2019-02-01 15:29:52 +08:00
parent e13e6cbca5
commit 7a213e6e76
2 changed files with 7 additions and 3 deletions

View File

@@ -206,7 +206,7 @@ func (slf *DBResult) mapSlice2interface(data []map[string]string, in interface{}
} }
// Connect ... // Connect ...
func (slf *DBModule) Connect() error { func (slf *DBModule) Connect(maxConn int) error {
cmd := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true&loc=%s", cmd := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true&loc=%s",
slf.UserName, slf.UserName,
slf.Password, slf.Password,
@@ -225,6 +225,10 @@ func (slf *DBModule) Connect() error {
return err return err
} }
slf.db = db slf.db = db
db.SetMaxOpenConns(maxConn)
db.SetMaxIdleConns(maxConn)
db.SetConnMaxLifetime(time.Second * 90)
return nil return nil
} }

View File

@@ -14,7 +14,7 @@ func TestDBModule(t *testing.T) {
Password: "Root!!2018", Password: "Root!!2018",
DBName: "QuantFundsDB", DBName: "QuantFundsDB",
} }
db.Connect() db.Connect(100)
res := db.Query("select * from tbl_fun_heelthrow where id >= 1") res := db.Query("select * from tbl_fun_heelthrow where id >= 1")
if res.Err != nil { if res.Err != nil {
@@ -26,7 +26,7 @@ func TestDBModule(t *testing.T) {
Uuid string `json:"uuid,omitempty"` Uuid string `json:"uuid,omitempty"`
AAAA string `json:"xxx"` AAAA string `json:"xxx"`
}{} }{}
err := res.SetSpecificTag("json").SetBlurMode(false).UnMarshal(&out) err := res.SetSpecificTag("json").SetBlurMode(true).UnMarshal(&out)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }