In go
I ran into the issue that the connection pool of sql.DB
pilled up so many connections it reached the limit of the odbc driver. The connections should be closed automatically but this never happens.
So I wanted to see what happens when I specifically ask for a connection, do my queries and close it in the end to give it back to the pool.
The following code gets stuck at the line err = conn.Close()
at the end. Can someone figure out why?
package main
import (
"database/sql"
"log"
"github.com/gin-gonic/gin"
)
var db *sql.DB
func main() {
db, err := sql.Open("odbc", "DSN=ADSconnStr")
if err != nil {
log.Fatal(err)
return
}
err = db.Ping()
if err != nil {
log.Fatal(err)
return
}
router := gin.Default()
router.GET("test/url", testQuery)
log.Fatal(router.Run(":8080"))
err = db.Close()
if err != nil {
log.Fatal(err)
}
}
func testQuery(c *gin.Context) {
ctx := c.Copy()
conn, err := db.Conn(ctx)
if err != nil {
log.Fatal(err)
return
}
rows, err := conn.QueryContext(ctx, "SELECT * FROM testTable;")
if err != nil {
log.Fatal(err)
return
}
defer rows.Close()
// scan results
// here it gets stuck. conn.Close never returns
err = conn.Close()
if err != nil {
log.Fatal(err)
return
}
}