From 531e16e4a326c2931c8053aff20259114de9a5e7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 27 Nov 2019 18:19:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86sql=20stmt?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=B2=A1=E6=9C=89=E5=85=B3=E9=97=AD=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/database/conn.go | 1 - server/dao/console-sqlite3/api.go | 2 ++ server/dao/console-sqlite3/apiStrategy.go | 7 +++++-- server/dao/console-sqlite3/config-log/log.go | 2 ++ server/dao/console-sqlite3/dao-balance/add.go | 3 +++ server/dao/console-sqlite3/dao-service/add.go | 2 +- server/dao/console-sqlite3/gateway.go | 2 ++ server/dao/console-sqlite3/node.go | 2 ++ server/dao/console-sqlite3/project.go | 1 + server/dao/console-sqlite3/strategy.go | 2 +- server/dao/console-sqlite3/strategyPlugin.go | 1 - 11 files changed, 19 insertions(+), 6 deletions(-) diff --git a/common/database/conn.go b/common/database/conn.go index ffdd332d..1a57a29a 100644 --- a/common/database/conn.go +++ b/common/database/conn.go @@ -58,7 +58,6 @@ func InitTable() error { if err != nil { Tx.Rollback() log.Error("InitTable error:",err,"\t sql:",sql) - panic(err) return err } } diff --git a/server/dao/console-sqlite3/api.go b/server/dao/console-sqlite3/api.go index 6a8cbb67..aebfdb01 100644 --- a/server/dao/console-sqlite3/api.go +++ b/server/dao/console-sqlite3/api.go @@ -356,6 +356,7 @@ func BatchEditAPIBalance(apiIDList []string, balance string) (string, error) { if err != nil { return "[ERROR]Fail to Prepare SQL!", err } + defer stmt.Close() _, err = stmt.Exec(now, now, balance) if err != nil { return "[ERROR]Fail to excute SQL statement!", err @@ -375,6 +376,7 @@ func BatchEditAPIGroup(apiIDList []string, groupID int) (string, error) { if err != nil { return "[ERROR]Fail to Prepare SQL!", err } + defer stmt.Close() _, err = stmt.Exec(now, now, groupID) if err != nil { return "[ERROR]Fail to excute SQL statement!", err diff --git a/server/dao/console-sqlite3/apiStrategy.go b/server/dao/console-sqlite3/apiStrategy.go index 3b9798f7..c85a74c5 100644 --- a/server/dao/console-sqlite3/apiStrategy.go +++ b/server/dao/console-sqlite3/apiStrategy.go @@ -20,6 +20,9 @@ func AddAPIToStrategy(apiList []string, strategyID string) (bool, string, error) stmt1, _ := Tx.Prepare(sql1) stmt2, _ := Tx.Prepare(sql2) stmt3, _ := Tx.Prepare(sql3) + defer stmt1.Close() + defer stmt2.Close() + defer stmt3.Close() for _, apiID := range apiList { id, err := strconv.Atoi(apiID) @@ -64,7 +67,7 @@ func SetAPITargetOfStrategy(apiID int, strategyID string, target string) (bool, if err != nil { return false, err.Error(), err } - + defer stmt.Close() _, e := stmt.Exec(target, apiID, strategyID) if e != nil { @@ -93,7 +96,7 @@ func BatchSetAPITargetOfStrategy(apiIds []int, strategyID string, target string) if err != nil { return false, err.Error(), err } - + defer stmt.Close() _, e := stmt.Exec(s...) if e != nil { diff --git a/server/dao/console-sqlite3/config-log/log.go b/server/dao/console-sqlite3/config-log/log.go index 15f0ccc4..805517a6 100644 --- a/server/dao/console-sqlite3/config-log/log.go +++ b/server/dao/console-sqlite3/config-log/log.go @@ -14,6 +14,7 @@ func Get(name string) (*entity.LogConfig, error) { if e != nil { return nil, e } + defer stmt.Close() ent := &entity.LogConfig{} err := stmt.QueryRow(name).Scan( &ent.Name, @@ -37,6 +38,7 @@ func Set(ent *entity.LogConfig) error { if e != nil { return e } + defer stmt.Close() _, err := stmt.Exec( ent.Name, ent.Enable, diff --git a/server/dao/console-sqlite3/dao-balance/add.go b/server/dao/console-sqlite3/dao-balance/add.go index 57abc93f..2c07e819 100644 --- a/server/dao/console-sqlite3/dao-balance/add.go +++ b/server/dao/console-sqlite3/dao-balance/add.go @@ -115,6 +115,7 @@ func Delete(name string) (string, error) { if err != nil { return "[ERROR]Illegal SQL statement!", err } + defer stmt.Close() _, err = stmt.Exec(name) if err != nil { return "[ERROR]DELETE fail", err @@ -132,10 +133,12 @@ func BatchDelete(balanceNames []string) (string, error) { if err != nil { return "[ERROR]Illegal SQL statement!", err } + defer stmt.Close() stmt2, err := db.Prepare(sql2) if err != nil { return "[ERROR]Illegal SQL statement!", err } + defer stmt2.Close() for _, balanceName := range balanceNames { stmt.Exec(balanceName) stmt2.Exec(balanceName) diff --git a/server/dao/console-sqlite3/dao-service/add.go b/server/dao/console-sqlite3/dao-service/add.go index bfc59b45..c5d11710 100644 --- a/server/dao/console-sqlite3/dao-service/add.go +++ b/server/dao/console-sqlite3/dao-service/add.go @@ -17,7 +17,7 @@ func Add(name, driver, desc, config, clusterConfig string, isDefault, healthChec if e != nil { return e } - + defer stmt.Close() _, err := stmt.Exec(name, driver, isDefault, desc, config, clusterConfig, healthCheck, healthCheckPath, healthCheckPeriod, healthCheckCode, healthCheckTimeOut, now, now) return err } diff --git a/server/dao/console-sqlite3/gateway.go b/server/dao/console-sqlite3/gateway.go index e77ec662..8c87ac96 100644 --- a/server/dao/console-sqlite3/gateway.go +++ b/server/dao/console-sqlite3/gateway.go @@ -38,6 +38,7 @@ func EditGatewayBaseConfig(successCode string, nodeUpdatePeriod, monitorUpdatePe if err != nil { return false, "[ERROR]Illegal SQL Statement!", err } + defer stmt.Close() _, err = stmt.Exec(successCode, nodeUpdatePeriod, monitorUpdatePeriod, monitorTimeout) if err != nil { return false, "[ERROR]Fail to excute SQL Statement!", err @@ -60,6 +61,7 @@ func EditGatewayAlarmConfig(apiAlertInfo, sender, senderPassword, smtpAddress st if err != nil { return false, "[ERROR]Illegal SQL Statement!", err } + defer stmt.Close() _, err = stmt.Exec(apiAlertInfo, alertStatus, sender, senderPassword, smtpAddress, smtpPort, smtpProtocol) if err != nil { return false, "[ERROR]Fail to excute SQL Statement!", err diff --git a/server/dao/console-sqlite3/node.go b/server/dao/console-sqlite3/node.go index 397492df..11ebf8e6 100644 --- a/server/dao/console-sqlite3/node.go +++ b/server/dao/console-sqlite3/node.go @@ -203,11 +203,13 @@ func UpdateAllNodeClusterID(clusterID int) { _, err := Tx.Exec(sql, clusterID) if err != nil { Tx.Rollback() + return } sql = "UPDATE goku_node_group SET clusterID = ?;" _, err = Tx.Exec(sql, clusterID) if err != nil { Tx.Rollback() + return } Tx.Commit() } diff --git a/server/dao/console-sqlite3/project.go b/server/dao/console-sqlite3/project.go index 2287263d..622e3822 100644 --- a/server/dao/console-sqlite3/project.go +++ b/server/dao/console-sqlite3/project.go @@ -54,6 +54,7 @@ func DeleteProject(projectID int) (bool, string, error) { sql := "SELECT groupID FROM goku_gateway_api_group WHERE projectID = ?;" rows, err := Tx.Query(sql, projectID) if err != nil { + Tx.Rollback() return false, "", err } //延时关闭Rows diff --git a/server/dao/console-sqlite3/strategy.go b/server/dao/console-sqlite3/strategy.go index 485f0600..e16b0fff 100644 --- a/server/dao/console-sqlite3/strategy.go +++ b/server/dao/console-sqlite3/strategy.go @@ -30,7 +30,7 @@ func AddStrategy(strategyName string, groupID int) (bool, string, error) { return false, "[ERROR]Empty strategy id !", nil } stmt, err := db.Prepare(`INSERT INTO goku_gateway_strategy (strategyID,strategyName,updateTime,createTime,groupID) VALUES (?,?,?,?,?);`) - + defer stmt.Close() if err != nil { return false, "[ERROR]Illegal SQL statement!", err } diff --git a/server/dao/console-sqlite3/strategyPlugin.go b/server/dao/console-sqlite3/strategyPlugin.go index f375b012..1cae721a 100644 --- a/server/dao/console-sqlite3/strategyPlugin.go +++ b/server/dao/console-sqlite3/strategyPlugin.go @@ -27,7 +27,6 @@ func AddPluginToStrategy(pluginName, config, strategyID string) (bool, interface result, err := Tx.Exec("INSERT INTO goku_conn_plugin_strategy (pluginName,pluginConfig,strategyID,createTime,updateTime,pluginStatus) VALUES (?,?,?,?,?,?);", pluginName, config, strategyID, now, now, 1) if err != nil { Tx.Rollback() - panic(err) return false, "[ERROR]Fail to insert data", errors.New("[ERROR]Fail to insert data") } connID, err := result.LastInsertId() -- GitLab