Golang中国

按顺序执行插入、更新、查询、删除,运行十几秒后报错太多连接,MySQL设置最大连接一万也不够,show processlist十秒钟不到就显示一万个sleep状态的连接,请教大神帮忙解释下我是有连接没关闭吗?
代码:

func insertUnis(db *sql.DB, name string){

stmt_insert, err := db.Prepare("insert into memDB1 values(?,?,'world.cnworld.cnworld.cnworld.cn',?,'ee02:123::af01:9231:df18:8998:ee02:123::af01:9231:df18:8998:abcd','2017070035|2703258|1943|0|0|10.121.205.248|088:abcd:e02:123::/64','Framed-Interface-Id`varchar(64)COLLATEutf8_binNOTNULLdefaultthah','varchar(64)COLLATEutf8_binNOTNULLdefault0xBF019231DF18899934hdas','Call-From-Id`varchar(32)COLLATEd',?,'Current-Bandwidt','NAS-Port-Idvarchar(256)COLLATEutf8_binNOTNULLdefaultsrunk 6/0/36:33.351 0/0/0/0/0/0NAS-Port-Idvarchar(256)COLLATEutf8_binNOTNULLdefaultsrunk 6/0/36:33.351 0/0/0/0/0/0NAS-Port-Idvarchar(256)COLLATEutf8_binNOTNULLdefaultsrunk 6/0/36:33.351 0/0/0/0/0/0sefiuhu',48204,57239,2458787490,2458787490,2458787490,'Static-Bandwidthvhar(24)','Bas-Bandwidth` varchar(64) COLLATE utf8_bin NOT NULL defaultegse','Session-Id varchar(64) COLLATE utf8_bin NOT NULL defaultlasefjie','Coa-Session-Id` varchar(64) COLLATE utf8_bin NOT NULL defaultsdf','macaddr`varchar(32) COLLATE utf8',?,65535,45656,'Reseverd1` varchar(32) COLLATE u',4294967292)")

if err != nil {
    Log(LL_ERROR, "stmt_insert Prepare err:", err.Error(), ".")
}
defer stmt_insert.Close()
stmt_update, err := db.Prepare("update memDB1 set user_name=? where session_key=?")
if err != nil {
    Log(LL_ERROR, "stmt_update Prepare err:", err.Error(), ".")
}    
defer stmt_update.Close()
stmt_del, err := db.Prepare("delete from memDB1 where session_key=?")
    if err != nil {
        Log(LL_ERROR, "stmt_del Prepare err:", err.Error(), ".")
}
defer stmt_del.Close() 
for i:=0;i<500000;i++{
    session_key:=string(Krand(64,3))
    user_name := string(Krand(64,3))
    frame_ip := string(Krand(32,3))        
    Nas_IP := string(Krand(32,3))    
    nat_ip := string(Krand(32,3))    

    _, err = stmt_insert.Exec(session_key,user_name,frame_ip,Nas_IP,nat_ip)
    if err != nil {
        Log(LL_ERROR, "stmt_insert Exec err:", err.Error(), ".")
    }

    _, err = stmt_update.Exec(string(Krand(64,3)), session_key)
    if err != nil {
        Log(LL_ERROR, "stmt_update Exec err:", err.Error(), ".")
    }
    sql := "SELECT * FROM memDB1 where session_key='" + session_key+"'"
    row, err := db.Query(sql)
    defer row.Close()
    if err != nil {
        Log(LL_ERROR, "Query err:", err.Error(), ".")
    }
    _, err = stmt_del.Exec(session_key)
     if err != nil {
        Log(LL_ERROR, "stmt_del Exec err:", err.Error(), ".")
    }
}
common_package.WgDay.Done()
return

}

show processlist:

| 1234491 | cid  | localhost:36979 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234492 | cid  | localhost:36980 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234493 | cid  | localhost:36981 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234494 | cid  | localhost:36982 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234495 | cid  | localhost:36983 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234496 | cid  | localhost:36984 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234497 | cid  | localhost:36985 | ciddb | Sleep   |    0 |          | NULL                  |
| 1234498 | cid  | localhost:36986 | ciddb | Sleep   |    0 |          | NULL                  |
+---------+------+-----------------+-------+---------+------+----------+-----------------------+
8040 rows in set (0.01 sec)

jimyokl 于 2017-05-16 15:46 修改
5 回复
jimmykuu
#1 jimmykuu • 2017-05-16 15:10

你的 defer stmt.Close() 都是在循环内,要等循环结束了才会调用关闭。

stmt 在循环外只创建一个就行了。

jimyokl
#2 jimyokl • 2017-05-16 15:23

谢谢站长回复

jimyokl
#3 jimyokl • 2017-05-16 15:48

改了以后还是报错:

2017-05-16 16:37:52 <LL_ERROR> stmt_del Exec err:Error 1040: Too many connections.
2017-05-16 16:37:52 <LL_ERROR> stmt_insert Exec err:Error 1040: Too many connections.
2017-05-16 16:37:52 <LL_ERROR> stmt_update Exec err:Error 1040: Too many connections.
2017-05-16 16:37:52 <LL_ERROR> Query err:Error 1040: Too many connections.

jimmykuu
#4 jimmykuu • 2017-05-16 18:03

循环里还有个查询的 defer row.Close()

clark
#5 clark • 2017-05-19 00:16

请教一下:
1、mysql库是用的哪个?

2、Krand函数是哪个包里面的?

需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。