13 Star 29 Fork 13

傅小黑 / GoInk

 / 详情

rows.Close misses in db.Query()

Backlog
Opened this issue  
2013-08-21 13:46

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.

Comments (5)

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

Status
Assignees
Milestones
Pull Requests
Successfully merging a pull request will close this issue.
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(2)
32 fuxiaohei 1642261381 10916 ckrissun 1578914735
Go
1
https://gitee.com/fuxiaohei/GoInk.git
git@gitee.com:fuxiaohei/GoInk.git
fuxiaohei
GoInk
GoInk

Search