我将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
}
一段时间后我得到了这个
无法创建超过max_prepared_stmt_count的语句(当前值:16382)
为什么会发生这种错误?
答案 0 :(得分:0)
我终于找到了! 有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
}