You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL插入报错:Cannot enqueue Quit after invoking quit 求助

解决MySQL插入操作中的"Cannot enqueue Quit after invoking quit"错误

问题根源

你的代码存在三个核心问题导致该错误:

  1. 重复关闭连接:connection.query的回调中,无论是否报错都会执行connection.end(),第一次关闭连接后,第二次调用会触发"重复关闭"的报错。
  2. 单连接不适合API场景:全局单连接无法应对并发请求,且每次调用agregarDatos都执行conectar,容易出现连接状态混乱。
  3. SQL注入风险:直接拼接参数生成SQL语句,不仅有安全隐患,还可能因参数格式问题引发语法错误。

修复方案

1. 修正连接关闭逻辑

确保connection.end()只执行一次,通过return避免重复调用:

connection.query(sql, function(error, res){
  if (error) {
    console.error('插入错误:', error);
    return connection.end(); // 报错后关闭连接并终止后续逻辑
  }
  console.log('数据插入成功:', res);
  connection.end();
})

2. 使用参数化查询避免SQL注入

改用mysql模块的?占位符传递参数,杜绝SQL注入:

const sql = `INSERT INTO u466684088_prueba_1.save_data 
            (account_number, campain_number, channel, offer_type, offer_sub_type, customerid)
            VALUES (?, ?, ?, ?, ?, ?)`;
connection.query(sql, [accoun_number, campain_number, channel, offer_type, offer_sub_type, customerid], function(error, res){
  if (error) {
    console.error('插入错误:', error);
    return connection.end();
  }
  console.log('数据插入成功:', res);
  connection.end();
})

3. 改用连接池适配API并发场景

单连接无法处理API的并发请求,推荐使用连接池自动管理连接的创建、复用与释放:

const mysql = require('mysql');

// 创建连接池
const pool = mysql.createPool({
  host: 'sql141.main-hosting.eu',
  database: 'u466684088_prueba_1',
  user: 'u466684088_prueba_1',
  password: 'Prueba_1prueba_1',
  connectionLimit: 10 // 根据业务需求调整连接数上限
});

// 封装插入函数
const agregarDatos = (accoun_number, campain_number, channel, offer_type, offer_sub_type, customerid) => {
  pool.getConnection((err, connection) => {
    if (err) {
      console.error('获取连接失败:', err);
      return;
    }
    const sql = `INSERT INTO u466684088_prueba_1.save_data 
                (account_number, campain_number, channel, offer_type, offer_sub_type, customerid)
                VALUES (?, ?, ?, ?, ?, ?)`;
    connection.query(sql, [accoun_number, campain_number, channel, offer_type, offer_sub_type, customerid], (error, res) => {
      // 无论成功失败,都将连接释放回池
      connection.release();
      if (error) {
        console.error('插入错误:', error);
        return;
      }
      console.log('数据插入成功:', res);
    });
  });
};

4. 优化API错误处理

为避免API崩溃,需在异步操作中添加完整的错误捕获。以Express框架为例:

// 先将agregarDatos改造为Promise版本
const agregarDatosPromise = (...params) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) return reject(err);
      const sql = `INSERT INTO u466684088_prueba_1.save_data 
                  (account_number, campain_number, channel, offer_type, offer_sub_type, customerid)
                  VALUES (?, ?, ?, ?, ?, ?)`;
      connection.query(sql, params, (error, res) => {
        connection.release();
        if (error) reject(error);
        else resolve(res);
      });
    });
  });
};

// Express路由示例
app.post('/agregar-datos', async (req, res) => {
  try {
    const { accoun_number, campain_number, channel, offer_type, offer_sub_type, customerid } = req.body;
    await agregarDatosPromise(accoun_number, campain_number, channel, offer_type, offer_sub_type, customerid);
    res.status(200).json({ message: '数据插入成功' });
  } catch (err) {
    console.error('API错误:', err);
    res.status(500).json({ error: '插入失败', details: err.message });
  }
});

内容的提问来源于stack exchange,提问作者DanielT22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 03:43:11