Var conn=services. NewConnection (utils. GetAppid (the req))
Defer func () {
_=conn. Close ()//release connections, or MYSQL connection will rise
If err: recover=(); err ! Nil={
FMT. Println (" GetQxList failure, ", err)
Rerr=FMT. Errorf (" % s ", err)
}
} ()
//pull the first layer
Rows, err:=conn. Query (SQLS, userid, "0" and "0");
if err ! Nil={
Panic (" query Error, "+ err. Error ())
}
For rows. Next () {
QxMenu1: MyJsonMemu={}
_=rows. Scan (& amp; QxMenu1 Qxid, & amp; QxMenu1. Name, & amp; QxMenu1 Pid, & amp; QxMenu1 Achk, & amp; QxMenu1. Visible)
//check 2
Rows1 err1:=conn. Query (SQLS, userid, QxMenu1. Qxid, QxMenu1 Qxid);
If err1==nil {
For rows1. Next () {
QxMenu2: Menu_2={}
_=rows1. Scan (& amp; QxMenu2 Qxid, & amp; QxMenu2. Name, & amp; QxMenu2 Pid, & amp; QxMenu2 Achk, & amp; QxMenu2. Visible)
//check three layer
Rows2 err2:=conn. Query (sqlsthree, userid, QxMenu2. Qxid, QxMenu2 Qxid);
If err2==nil {
For rows2. Next () {
QxMenu3: Menu_3={}
_=rows2. Scan (& amp; QxMenu3 Qxid, & amp; QxMenu3. Name, & amp; QxMenu3 Pid, & amp; QxMenu3 Achk, & amp; QxMenu3. Visible)
QxMenu2. Subs=append (QxMenu2 Subs, QxMenu3)
}
}
_=rows2. Close ()
QxMenu1. Subs=append (QxMenu1 Subs, QxMenu2)
}
}
_=rows1. Close ()
QxList=append (QxList QxMenu1)
}
_=rows. The Close ()
Rjso, _ :=json. Marshal (QxList)
_=json. The Unmarshal (byte [] (rjso), & amp; Result. The result)
}
The code above I want to achieve level cyclic query the database according to the father and son to return to, if the code does not add rows. The Close () calls a MYSQL to add three connection and will never release,
Unless you restart the service, I think a problem to have a more convenient database connection release method, so that every time to write in the back rows. The Close () feel very trouble, forget to write down or error code loop guide to one thousand didn't perform to release code and will not lead to connect their cause crashes, don't know how the eldest brother is done,
CodePudding user response:
Defer is not do it?For example,
Rows, err:=conn. Query (SQLS, userid, "0" and "0");
if err ! Nil={
Panic (" query Error, "+ err. Error ())
}
defer rows. The Close ()
For rows. Next () {
QxMenu1: MyJsonMemu={}
_=rows. Scan (& amp; QxMenu1 Qxid, & amp; QxMenu1. Name, & amp; QxMenu1 Pid, & amp; QxMenu1 Achk, & amp; QxMenu1. Visible)
//check 2
Rows1 err1:=conn. Query (SQLS, userid, QxMenu1. Qxid, QxMenu1 Qxid);
If err1 {
The continue
}
defer rows1. Close ()
For rows1. Next () {
QxMenu2: Menu_2={}
_=rows1. Scan (& amp; QxMenu2 Qxid, & amp; QxMenu2. Name, & amp; QxMenu2 Pid, & amp; QxMenu2 Achk, & amp; QxMenu2. Visible)
//check three layer
Rows2 err2:=conn. Query (sqlsthree, userid, QxMenu2. Qxid, QxMenu2 Qxid);
If err2 {
The continue
}
defer rows2. Close ()
For rows2. Next () {
.
CodePudding user response:
Upstairs is the solution, you can use the defer, you can put row1, row2 defined in advance, use assignment below, rather than a quick definition, and then to defer the close function