Multiple goroutine with access to Oracle bank

We have a service that executes several SQL in an infinite loop with a pause every 5 seconds. I would like to know if I am doing the correct procedure in relation to the treatment of the database. I created a global variable in which, when starting the service, we also started the connection with the bank and saved it in this global variable. Then for the executions of SQL, I created a local variable to execute a query or an update, delete, insert(DML) and these local variables are taking the global variable database to execute.

Here is the code:

type GERENCIACON struct {
  DataBase *sql.DB
}

func (gc *GERENCIACON) F_FECHAR_CONEXAO() {
  gc.DataBase.Close()
}

func (gc *GERENCIACON) F_ABRIR_CONEXAO()  {	
if gc.DataBase == nil || gc.DataBase.Ping() != nil {	
	gc.DataBase, _ = sql.Open("goracle", "XXXX/XXXX@10.0.254.10:1521/orcl")
}
}

var VGGerenciaConexao GERENCIACON

The global variable is VGGerenciaCON

Now follows the code of the local variables for consultation and dml that I am creating in the codes:

  type GERENCIACONSULTA struct {
DataBase *sql.DB
Rows *sql.Rows
}

func (gc *GERENCIACONSULTA) F_EXECUTA_CONSULTA(pSql string) {
VGGerenciaConexao.F_ABRIR_CONEXAO()
gc.DataBase = VGGerenciaConexao.DataBase
gc.Rows, _ = gc.DataBase.Query(pSql)
}

type GERENCIADML struct {
DataBase *sql.DB
Stmt *sql.Stmt
Result sql.Result
Error error
}

func (gd *GERENCIADML) F_EXECUTA_DML(pSql string) {
VGGerenciaConexao.F_ABRIR_CONEXAO()
gd.DataBase = VGGerenciaConexao.DataBase
gd.Stmt, gd.Error = gd.DataBase.Prepare(pSql)
gd.Result, gd.Error = gd.Stmt.Exec()
}

As you can see I check the connection to the global variable and start it in case of an error and then assign the value of the database to the local variable. I use this structure in all services.

Is it okay to do that? I’m having some crashes so I’m still trying to find out about it.

It is better to have one routine to return your database connection object, use in your goroutine as a local var and then clos ethat connection in a defer…
I think sql.DB already handle a pool for database connection