From db2b1c2e129e04177cb3c3c014387f1d3ffe82d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AF=93=E7=90=AA?= Date: Tue, 19 Sep 2023 17:50:21 +0800 Subject: [PATCH 1/2] close db when insert finished --- pkg/telemetryservice/sql/mysql/mysql.go | 2 +- pkg/telemetryservice/sql/sqlserver/sqlserver.go | 2 +- pkg/telemetryservice/sql/tdengine/tdengine.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/telemetryservice/sql/mysql/mysql.go b/pkg/telemetryservice/sql/mysql/mysql.go index 0f4b47f50..800aad138 100644 --- a/pkg/telemetryservice/sql/mysql/mysql.go +++ b/pkg/telemetryservice/sql/mysql/mysql.go @@ -41,7 +41,7 @@ func (db *DBHelper) SendToDB(ctx context.Context, deviceName string, rawData []b return err } - return nil + return db.DB.Close() } func constructDBUri(sqlcs *v1alpha1.SQLConnectionSetting) string { diff --git a/pkg/telemetryservice/sql/sqlserver/sqlserver.go b/pkg/telemetryservice/sql/sqlserver/sqlserver.go index c661798b8..0a0bd6b19 100644 --- a/pkg/telemetryservice/sql/sqlserver/sqlserver.go +++ b/pkg/telemetryservice/sql/sqlserver/sqlserver.go @@ -41,7 +41,7 @@ func (db *DBHelper) SendToDB(ctx context.Context, deviceName string, rawData []b return err } - return nil + return db.DB.Close() } func constructDBUri(sqlcs *v1alpha1.SQLConnectionSetting) string { diff --git a/pkg/telemetryservice/sql/tdengine/tdengine.go b/pkg/telemetryservice/sql/tdengine/tdengine.go index 70acfc4c4..de5a5a14d 100644 --- a/pkg/telemetryservice/sql/tdengine/tdengine.go +++ b/pkg/telemetryservice/sql/tdengine/tdengine.go @@ -34,7 +34,7 @@ func (db *DBHelper) SendToDB(ctx context.Context, deviceName string, rawData []b return err } - return nil + return db.DB.Close() } func (db *DBHelper) ConnectToDB(ctx context.Context) error { From 03947521a4a645264ea531ea8a22987a92c4d48a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AF=93=E7=90=AA?= Date: Tue, 19 Sep 2023 20:03:49 +0800 Subject: [PATCH 2/2] use defer instead of return db.close --- pkg/telemetryservice/sql/mysql/mysql.go | 4 +++- pkg/telemetryservice/sql/sqlserver/sqlserver.go | 4 +++- pkg/telemetryservice/sql/tdengine/tdengine.go | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/telemetryservice/sql/mysql/mysql.go b/pkg/telemetryservice/sql/mysql/mysql.go index 800aad138..cbdefacd0 100644 --- a/pkg/telemetryservice/sql/mysql/mysql.go +++ b/pkg/telemetryservice/sql/mysql/mysql.go @@ -35,13 +35,15 @@ func (db *DBHelper) SendToDB(ctx context.Context, deviceName string, rawData []b return err } + defer db.DB.Close() + err = db.InsertDataToDB(ctx, deviceName, rawData) if err != nil { logger.Errorf("Error to Insert rawData to DB, errror: %v", err.Error()) return err } - return db.DB.Close() + return nil } func constructDBUri(sqlcs *v1alpha1.SQLConnectionSetting) string { diff --git a/pkg/telemetryservice/sql/sqlserver/sqlserver.go b/pkg/telemetryservice/sql/sqlserver/sqlserver.go index 0a0bd6b19..2f2f4e791 100644 --- a/pkg/telemetryservice/sql/sqlserver/sqlserver.go +++ b/pkg/telemetryservice/sql/sqlserver/sqlserver.go @@ -35,13 +35,15 @@ func (db *DBHelper) SendToDB(ctx context.Context, deviceName string, rawData []b return err } + defer db.DB.Close() + err = db.InsertDataToDB(ctx, deviceName, rawData) if err != nil { logger.Errorf("Error to Insert rawData to DB, errror: %v", err.Error()) return err } - return db.DB.Close() + return nil } func constructDBUri(sqlcs *v1alpha1.SQLConnectionSetting) string { diff --git a/pkg/telemetryservice/sql/tdengine/tdengine.go b/pkg/telemetryservice/sql/tdengine/tdengine.go index de5a5a14d..e603f0b20 100644 --- a/pkg/telemetryservice/sql/tdengine/tdengine.go +++ b/pkg/telemetryservice/sql/tdengine/tdengine.go @@ -28,13 +28,15 @@ func (db *DBHelper) SendToDB(ctx context.Context, deviceName string, rawData []b return err } + defer db.DB.Close() + err = db.InsertDataToDB(ctx, deviceName, rawData) if err != nil { logger.Errorf("Error to Insert rawData to DB, errror: %v", err.Error()) return err } - return db.DB.Close() + return nil } func (db *DBHelper) ConnectToDB(ctx context.Context) error {