There is no rows.Close called in db.Query method(in db.go file), maybe a "too many connection" error will arise in database operation.
Please check it, thanks.
ok... I'll check it.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
I add a defer func to close db like that :
# database/db.go:32
rows, e := this.sqlDb.Query(sql, args...)
if e != nil {
panic(fmt.Sprint(e, " # ", sql))
}
defer this.sqlDb.Close()
......
If I do query several times, such as:
db.Query(sql1)
db.Query(sql2)
It will panic when running sql2 and show sql: database is closed # SELECT .....
And the db connections must be set with SetMaxIdleConns
method, they should be managed by go's db pool.
ok, got it. Maybe I only use one *sql.DB object, so I must call rows.Close() when Query() is called.
But I think, the frequent open and close of *sql.DB object is not a good idea.
you can read my app
package. It's a web app sample with hxgo
components. *sql.DB
object is a global object in app
and initilized in main process. When a http goroutine call, the db should connect and do query. Because of the max connections limit, *sql.DB
can create proper connections and apply to each gorourine. so it shouldn't close.
Sign in to comment