为什么 defer stmnt.Close() 似乎阻止了我的 http.Redirect?

为什么我defer stmnt.Close()似乎阻止我http.Redirect重定向它只是挂在网站上无限尝试加载。


但是如果我删除defer stmnt.Close()它重定向就好了?


    err = db.QueryRow("SELECT steamid FROM accounts WHERE steamid = ?", ids).Scan(&steamid)

    if err != nil {

        common.WriteLog(err.Error(), r)

        http.Error(w, "Failed to connect to database. Try again in a bit.", 500)

    }


    switch {

    case len(profile.Response.Players) == 0:

        common.WriteLog("Failed to look you up in the steam database. Try again in a bit.", r)

        http.Error(w, "Failed to look you up in the steam database. Try again in a bit.", 500)

    case err == sql.ErrNoRows:

        stmnt, err := db.Query("INSERT INTO accounts SET steamid=?", ids)

        if err != nil {

            common.WriteLog(err.Error(), r)

            http.Error(w, "Failed to insert your account to the database. Try again in a bit.", 500)

        }

        defer stmnt.Close() // <<<<< The suspect

        // Insert Account


        http.Redirect(w, r, "/", 303)

    case err != nil:

        common.WriteLog(err.Error(), r)

        http.Error(w, "Failed to insert your account to the database. Try again in a bit.", 500)


    default:

        // Login User


        http.Redirect(w, r, "/", 303)


    }


达令说
浏览 162回答 1
1回答

海绵宝宝撒

使用db.Exec代替db.Query。Exec 执行查询而不返回任何行。对比Query 执行返回行的查询至于为什么,我猜mysqlRows.Close正在等待连接上的数据:func (rows *mysqlRows) Close() error {&nbsp; &nbsp; mc := rows.mc&nbsp; &nbsp; if mc == nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; if mc.netConn == nil {&nbsp; &nbsp; &nbsp; &nbsp; return ErrInvalidConn&nbsp; &nbsp; }&nbsp; &nbsp; // Remove unread packets from stream&nbsp; &nbsp; err := mc.readUntilEOF()&nbsp; &nbsp; rows.mc = nil&nbsp; &nbsp; return err}这是永远不会发生的。请参阅此示例。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go