此前发现golang mysql连接池无法释放,后定位了在rows.Next()循环中使用mysql exec函数将会引起rows.close无法释放链接,请问有遇到这个问题的吗? 今天专门写段测试代码,100%复现: func test() {
db := Kit.Mysql("dbname")

rows, err := db.Query("select uid,openid,wechat_id,nickname from test where 1 limit 5")
for rows.Next() {
var uid int
var openid string
var wechat_id string
var nickname string
err = rows.Scan(&uid, &openid, &wechat_id, &nickname)
if err != nil {
continue
}
fmt.Println(uid, openid, wechat_id, nickname)

if !strings.Contains(nickname, "__") {
nickname = nickname + "__"
} else {
nickname = strings.Replace(nickname, "__", "", -1)
}


db.Exec("update test set nickname=? where uid=? ", nickname, uid)


}
rows.Close()
}

func main(){

for i := 0; i < 300; i++ {
go test()
}
time.Sleep(time.Second * 2)
//由于连接池最大限制是200个,资源被耗尽,这段代码执行时一直卡在这里
test()
time.Sleep(time.Second * 20)

}