Merge pull request #19 from 30x/ISSUE-65205927

[ISSUE-65205927] Use transaction for writing, add row close
diff --git a/apidAnalytics_suite_test.go b/apidAnalytics_suite_test.go
index 983a987..669b18c 100644
--- a/apidAnalytics_suite_test.go
+++ b/apidAnalytics_suite_test.go
@@ -119,7 +119,10 @@
 }
 
 func createTables(db apid.DB) {
-	_, err := db.Exec(`
+	tx, err := db.Begin()
+	Expect(err).Should(Succeed())
+	defer tx.Rollback()
+	_, err = tx.Exec(`
 		CREATE TABLE IF NOT EXISTS kms_api_product (
 		    id text,
 		    tenant_id text,
@@ -186,13 +189,16 @@
 		    PRIMARY KEY (appcred_id, app_id, apiprdt_id,tenant_id)
 		);
 	`)
-	if err != nil {
-		panic("Unable to initialize DB " + err.Error())
-	}
+	Expect(err).Should(Succeed())
+	err = tx.Commit()
+	Expect(err).Should(Succeed())
 }
 
 func createApidClusterTables(db apid.DB) {
-	_, err := db.Exec(`
+	txn, err := db.Begin()
+	Expect(err).Should(Succeed())
+	defer txn.Rollback()
+	_, err = txn.Exec(`
 		CREATE TABLE edgex_apid_cluster (
 		    id text,
 		    instance_id text,
@@ -222,16 +228,17 @@
 		    PRIMARY KEY (id)
 		);
 	`)
-	if err != nil {
-		panic("Unable to initialize DB " + err.Error())
-	}
+	Expect(err).Should(Succeed())
+	err = txn.Commit()
+	Expect(err).Should(Succeed())
 }
 
 func insertTestData(db apid.DB) {
 
 	txn, err := db.Begin()
-	Expect(err).ShouldNot(HaveOccurred())
-	txn.Exec("INSERT INTO kms_app_credential_apiproduct_mapper (tenant_id,"+
+	Expect(err).Should(Succeed())
+	defer txn.Rollback()
+	_, err = txn.Exec("INSERT INTO kms_app_credential_apiproduct_mapper (tenant_id,"+
 		" appcred_id, app_id, apiprdt_id, status, _change_selector) "+
 		"VALUES"+
 		"($1,$2,$3,$4,$5,$6)",
@@ -242,8 +249,8 @@
 		"APPROVED",
 		"12345",
 	)
-
-	txn.Exec("INSERT INTO kms_app (id, tenant_id, name, developer_id) "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO kms_app (id, tenant_id, name, developer_id) "+
 		"VALUES"+
 		"($1,$2,$3,$4)",
 		"testappid",
@@ -251,16 +258,16 @@
 		"testapp",
 		"testdeveloperid",
 	)
-
-	txn.Exec("INSERT INTO kms_api_product (id, tenant_id, name) "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO kms_api_product (id, tenant_id, name) "+
 		"VALUES"+
 		"($1,$2,$3)",
 		"testproductid",
 		"tenantid",
 		"testproduct",
 	)
-
-	txn.Exec("INSERT INTO kms_developer (id, tenant_id, username, email) "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO kms_developer (id, tenant_id, username, email) "+
 		"VALUES"+
 		"($1,$2,$3,$4)",
 		"testdeveloperid",
@@ -268,8 +275,8 @@
 		"testdeveloper",
 		"testdeveloper@test.com",
 	)
-
-	txn.Exec("INSERT INTO edgex_data_scope (id, _change_selector, "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO edgex_data_scope (id, _change_selector, "+
 		"apid_cluster_id, scope, org, env) "+
 		"VALUES"+
 		"($1,$2,$3,$4,$5,$6)",
@@ -280,7 +287,9 @@
 		"testorg",
 		"testenv",
 	)
-	txn.Commit()
+	Expect(err).Should(Succeed())
+	err = txn.Commit()
+	Expect(err).Should(Succeed())
 }
 
 var _ = AfterSuite(func() {
diff --git a/common_helper.go b/common_helper.go
index 05805bf..262546a 100644
--- a/common_helper.go
+++ b/common_helper.go
@@ -186,7 +186,7 @@
 	db := getDB()
 	rows, err := db.Query("SELECT 1 FROM edgex_data_scope"+
 		" where org = ? and env = ?", tenant.Org, tenant.Env)
-
+	defer rows.Close()
 	if !rows.Next() {
 		if err == nil {
 			reason := "No tenant found for this org: " + tenant.Org + " and env:" + tenant.Env