DB connection inside a go routine [solved]

Hey there, I put my code inside this Windows service example and I started getting an error saying that no odbc driver or datasource name was found when trying to connect, but it’s there, below my code;

package main

import (
	"github.com/kardianos/service"

	"net"

	"strings"

	"hlsat.com.br/funcoes_gerais"

	"database/sql"
	"strconv"
	"time"
)

var logger service.Logger
var __db *sql.DB

type program struct{}

func rotinaPrincipalService(db *sql.DB) {
	// ativa o servidor udp --------------------------------------------------------

	ServerAddr, err := net.ResolveUDPAddr("udp", ":2008")
	CheckError(err)
	ServerConn, err := net.ListenUDP("udp", ServerAddr)
	CheckError(err)
	defer ServerConn.Close()
	buf := make([]byte, 1024)

	// ativa o servidor udp --------------------------------------------------------

	for {
		//udp routines here........................
	} //FIM LOOP SERVICO UDP..........................

}
func (p *program) Stop(s service.Service) error {
	// Stop should not block. Return with a few seconds.
	return nil
}

func main() {
	svcConfig := &service.Config{
		Name:        "ServicoSuntechTeste5",
		DisplayName: "ServicoSuntechTeste5",
		Description: "Teste servico...",
	}

	prg := &program{}
	s, err := service.New(prg, svcConfig)
	if err != nil {
		GeraLog(err.Error())
	}
	logger, err = s.Logger(nil)
	if err != nil {
		GeraLog(err.Error())
	}
	err = s.Run()
	if err != nil {
		GeraLog(err.Error())
	}
}

func (p *program) Start(s service.Service) error {
	// Start should not block. Do the actual work async.

	// carrega configuracoes iniciais e conecta no banco ---------------------------

	LoadConfig()
	__db = ConectaBanco()

	// carrega configuracoes iniciais e conecta no banco ---------------------------

	go p.run()
	return nil
}
func (p *program) run() {
	// Do work here
	rotinaPrincipalService(__db)
}

My connect function

func ConectaBanco() *sql.DB {
	Rst, err := sql.Open("odbc", "DSN="+cfg.BdDsn+";UID="+cfg.BdUsr+";PWD="+cfg.BdSnh)
	CheckError(err)
	return Rst
}

And the import;

_ "github.com/alexbrainman/odbc"

All my code works fine, this code was shorted to show only where is the problem I’m passing through, if I get the code out of this service it works perfectly… Anyone knows why it happens?

[EDIT]
Digging on Google I found this post where the guy is passing through the same situation I guess, but with Oracle driver, one of the guys replied that the driver should be initialized to support multiple threads, how can I do this with this ODBC driver I’m using, to try fix it? Below the link to the post;

https://github.com/mattn/go-oci8/issues/32

Tnx.

If all the tests of alexbrainman/odbc pass (https://github.com/alexbrainman/odbc/wiki/RunningTests), then please raose this issue on that repo!

1 Like

check if you have the right permissions for accessing the odbc.

1 Like

But my application works normally outside de goroutine, everything works fine, it just stop working when I try to do it inside a goroutine…

it could be that the underlying .dll on windows expects to be receiving calls on the “main” OS thread (or, always on the same one.)

but a goroutine can be executed one time on a given OS-thread, and, the next time, on a completely different one.
“foreign” libraries aren’t used to that, or, as said above, expect to always be called from the main OS thread (like, e.g. OpenGL).

the usual way to workaround this is to associate a goroutine to a given OS-thread (see runtime.LockOSThread):

more infos: https://github.com/golang/go/wiki/LockOSThread

hth,
-s

1 Like

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