gpt4 book ai didi

gorose 不能创建超过 max_prepared_stmt_count 个语句

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:54 26 4
gpt4 key购买 nike

我将 gorose 用于带有 golang 的 web 项目,代码如下

var tablecheckrequest = "checkrequest"

func (mysqldao *MysqlDao) GetAllCheckRulesByRequestId(id int) []map[string]interface{} {
result, _ := mysqldao.connection.Table(tablecheckrequest).
Where("requestid", "=", id).Get()
return result
}

一段时间后我明白了

Can't create more than max_prepared_stmt_count statements (current value: 16382)

为什么会出现这个错误?

最佳答案

我终于知道了!有gorose的源码

func (dba *Database) Execute(args ...interface{}) (int64, error) {
//defer DB.Close()
lenArgs := len(args)
var sqlstring string
var vals []interface{}

sqlstring = args[0].(string)

if lenArgs > 1 {
for k, v := range args {
if k > 0 {
vals = append(vals, v)
}
}
}
// 记录sqllog
//Connect.SqlLog = append(Connect.SqlLog, fmt.Sprintf(sqlstring, vals...))
dba.LastSql = fmt.Sprintf(sqlstring, vals...)
dba.SqlLogs = append(dba.SqlLogs, dba.LastSql)

var operType string = strings.ToLower(sqlstring[0:6])
if operType == "select" {
return 0, errors.New("this method does not allow select operations, use Query")
}

if dba.trans == true {
stmt, err := tx.Prepare(sqlstring)
if err != nil {
return 0, err
}
return dba.parseExecute(stmt, operType, vals)
}

stmt, err := DB.Prepare(sqlstring)
if err != nil {
return 0, err
}
return dba.parseExecute(stmt, operType, vals)
}

在此方法中,stmt 已创建但从未关闭!要修复它,我们只需要添加一个延迟 stmt.close() durging 方法 parseExecute() 就像

func (dba *Database) parseExecute(stmt *sql.Stmt, operType string, vals []interface{}) (int64, error) {
defer stmt.Close()
var rowsAffected int64
var err error
result, errs := stmt.Exec(vals...)
if errs != nil {
return 0, errs
}

switch operType {
case "insert":
// get rows affected
rowsAffected, err = result.RowsAffected()
dba.RowsAffected = int(rowsAffected)
// get last insert id
rowsAffected, err = result.LastInsertId()
dba.LastInsertId = int(rowsAffected)
case "update":
rowsAffected, err = result.RowsAffected()
case "delete":
rowsAffected, err = result.RowsAffected()
}

return rowsAffected, err
}

关于gorose 不能创建超过 max_prepared_stmt_count 个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com