I'm working in a golang project using sqlx and postgres. When the application starts I open a connection with the database and use it like this:
var connRO *sqlx.DB
var connRW *sqlx.DB
...
/ GetInstance - will return the connection opened to the database
func GetInstance(readonly bool) *sqlx.DB {
if readonly {
return connRO
}
return connRW
}
The problem is that in some blocks of code the connection still in use, here one example:
instanceRW := database.GetInstance(false)
instanceRO := database.GetInstance(true)
...
err := instanceRO.Get(&idFuncionario, `
SELECT id
FROM t_funcionario
WHERE codigo_externo = $1 `,
i.FuncionarioID)
if err != nil {
log.Println(err)
return errors.New("Erro ao identificar funcionário.")
}
// Verifica se o item é granel
// Caso não seja
if *i.ItemGranelID == 0 {
// Verifica se o item está disponível
err = instanceRO.Get(&localidade_id, `
SELECT COALESCE(localidade_id, 0)
FROM t_item
WHERE id = $1
`, i.ItemID)
if err != nil {
log.Println(err)
return errors.New("Não foi possível identificar tipo do item.")
...
}
When I try test it doing something like it, the test don't open many connections. Here some test code:
i := 600
for i != 0 {
if true {
err := db.Select(&item, `SELECT * FROM t_item LIMIT 10`)
if err != nil {
}
err = db.Select(&categoria, `SELECT * FROM t_categoria LIMIT 10`)
if err != nil {
}
err = db.QueryRow(`INSERT INTO t_categoria
(
nome,
ativo
)
VALUES ($1, $2)`, fmt.Sprintf("cateToTeste%v", i), true).Scan(&itemget)
if err != nil {
}
err = db.Get(&itemget, `SELECT COALESCE(localidade_id, 0)
FROM t_item
WHERE id = $1`, 150)
if err != nil {
}
log.Println("ok")
i--
}
}
log.Println("Tudo ok!!")
Some time I get max connections and the application die.