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.