我正在使用带有连接池和连接限制为 10 的 node-mysql2。当我重新启动应用程序时,结果很好 - 它们与我在 db 上的匹配。但是当我开始插入新记录并重做相同的选择查询时,我会得到间歇性的结果,缺少我刚刚添加的最新记录。
如果我直接检查数据库,我可以看到我刚刚通过我的应用程序添加的记录。只是应用程序无法以某种方式看到它。
我认为这是一个错误,但这是我的代码设置方式:
module.exports.getDB = function (dbName) {
if (!(dbName in dbs)) {
console.log(`Initiating ${dbName}`);
let config = dbConfigs[dbName];
dbs[dbName] = mysql.createPool({
host: config.host,
port: config.port || 3306,
user: config.user,
password: config.password,
connectionLimit: 10,
database: config.database,
debug: config.debug
});
}
return dbs[dbName]; // I just initialize each database once
};
这是我的选择查询:
let db = dbs.getDB('myDb');
const [rows] = await db.query(`my query`);
console.log(rows[0]); // this one starts to show my results inconsistently once I insert records
这是我的插入查询:
module.exports = {
addNote: async function(action, note, userID, expID) {
let db = dbs.getDB('myDb');
await db.query(`INSERT INTO experiment_notes (experiment_id, action, created_by, note)
VALUES (?, ?, ?, ?)`, [expID, action, userID, note]);
}
};
如果我将 connectionLimit 设置为 1,我将无法重现问题……至少现在还没有
知道我做错了什么吗?