Store Multiple DB Connection in golang

Hi Team,

I have 10 oracle databases in one server, I am trying to store db connection of all of them but when I am trying to execute query I am getting the result from one database 10 times. Please help.

type dbConn struct {
db *sql.DB
}
func oracledb_sql_connection(orcldb_repo_data []string, db_home_bin string, logfile string, debug bool) []dbConn {
	var inst_temp, oracle_home, ld_library_path, path string
	var db_connection_arr []dbConn
	for _, j := range orcldb_repo_data {
		for i := 0; i <= 2; i++ {
			inst_temp = j[:len(j)-i]
			ora_result, _ := exec.Command("grep", inst_temp, "/etc/oratab").Output()
			if len(ora_result) > 0 {
				inst_temp = fmt.Sprint(strings.TrimSpace(strings.Split(string(ora_result), ":")[0]))
				break
			}
		}

		//Setting DB Enviornment
		os.Setenv("ORACLE_SID", inst_temp)
		orcldb_home, err := exec.Command("sh", db_home_bin, inst_temp).Output()
		if err != nil {
			break
		}
		oracle_home = fmt.Sprint(strings.TrimSpace(string(orcldb_home)))
		os.Setenv("ORACLE_HOME", oracle_home)
		ld_library_path = os.Getenv("ORACLE_HOME") + "/lib"
		os.Setenv("LD_LIBRARY_PATH", ld_library_path)
		if !strings.Contains(os.Getenv("PATH"), os.Getenv("ORACLE_HOME")) {
			path = os.Getenv("PATH") + ":" + os.Getenv("ORACLE_HOME") + "/bin"
			os.Setenv("PATH", path)
		}
		os.Setenv("ORACLE_SID", j)

		////////////////////////////////////

		//Connection to database
		connect_string := "/ as sysdba"
		db, err := sql.Open("godror", connect_string)
		if err != nil {
			break
		}
		//defer db.Close()
		fmt.Println(db)
		fmt.Println("DB Connection Open")
		db.Conn()
		db_connection_arr = append(db_connection_arr, dbConn{db})
	}

	return db_connection_arr
}


You’re calling sql.Open with the same connection string on every iteration. Perhaps you believe that setting the environment variable ORACLE_SID would affect the results, maybe I’m missing something, but I see nothing in the documentation for the driver about it using an environment variable for making a connection.

I am using it sequentially without storing db connection and it’s working . But when I am trying to store the db connection it’s not.

There is something while storing db connection which we need to figure out.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.