doujia7779 2015-09-04 00:39
浏览 28
已采纳

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

Why does my defer stmnt.Close() seem to block my http.Redirect from redirecting it just hangs on the website infinitely trying to load.

But if I remove the defer stmnt.Close() it redirects just fine?

    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)

    }
  • 写回答

1条回答 默认 最新

  • doushi7314 2015-09-04 00:52
    关注

    Use db.Exec instead of db.Query.

    Exec executes a query without returning any rows.

    vs

    Query executes a query that returns rows

    As for why, I would guess the mysql Rows.Close is waiting for data on the connection:

    func (rows *mysqlRows) Close() error {
        mc := rows.mc
        if mc == nil {
            return nil
        }
        if mc.netConn == nil {
            return ErrInvalidConn
        }
    
        // Remove unread packets from stream
        err := mc.readUntilEOF()
        rows.mc = nil
        return err
    }
    

    Which is never going to happen.

    See this for example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 基于同花顺supermind的量化策略脚本编辑
  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?