From 484af88fed171396a9b8eb1134e35a6e037420b8 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Sat, 22 Dec 2018 00:49:21 +0800 Subject: [PATCH] connection reuse --- database/mysql.go | 18 +++++++----------- env/env.go | 6 ++++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/database/mysql.go b/database/mysql.go index 0db911e..ce7cbd0 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -134,22 +134,18 @@ func (db *Connector) Version() (int, error) { // MySQL https://dev.mysql.com/doc/refman/8.0/en/comments.html var versionStr string var versionSeg []string - for res.Rows.Next() { + if res.Rows.Next() { err = res.Rows.Scan(&versionStr) - if err != nil { - break - } - versionStr = strings.Split(versionStr, "-")[0] - versionSeg = strings.Split(versionStr, ".") - if len(versionSeg) == 3 { - versionStr = fmt.Sprintf("%s%02s%02s", versionSeg[0], versionSeg[1], versionSeg[2]) - version, err = strconv.Atoi(versionStr) - } - break } if err := res.Rows.Close(); err != nil { common.Log.Error(err.Error()) } + versionStr = strings.Split(versionStr, "-")[0] + versionSeg = strings.Split(versionStr, ".") + if len(versionSeg) == 3 { + versionStr = fmt.Sprintf("%s%02s%02s", versionSeg[0], versionSeg[1], versionSeg[2]) + version, err = strconv.Atoi(versionStr) + } return version, err } diff --git a/env/env.go b/env/env.go index 204b62b..336152b 100644 --- a/env/env.go +++ b/env/env.go @@ -367,11 +367,12 @@ func (ve VirtualEnv) createDatabase(rEnv *database.Connector, dbName string) err if ddl == "" { return fmt.Errorf("dbName: '%s' get create info error", dbName) } - _, err = ve.Query(ddl) + res, err := ve.Query(ddl) if err != nil { common.Log.Warning("createDatabase, Error : %v", err) return err } + res.Rows.Close() // 创建成功,添加映射记录 ve.DBRef[dbName] = dbHash @@ -449,12 +450,13 @@ func (ve VirtualEnv) createTable(rEnv *database.Connector, dbName, tbName string // 改变数据环境 ve.Database = ve.DBRef[dbName] - _, err = ve.Query(ddl) + res, err := ve.Query(ddl) if err != nil { // 有可能是用户新建表,因此线上环境查不到 common.Log.Error("createTable, %s Error : %v", tbName, err) return err } + res.Rows.Close() // 泵取数据 if common.Config.Sampling { -- GitLab