【问题标题】:Why am I getting a timeout error when exporting a mariadb connection pool in Node.js?为什么在 Node.js 中导出 mariadb 连接池时出现超时错误?
【发布时间】:2022-11-05 01:01:11
【问题描述】:

编辑

我发现了错误。错误非常明显:我没有包括 require("dotenv").config(); 在 connection.js 文件中。如果没有这个,数据库连接会在超时后简单地失败,因为它没有任何连接细节。 我发现 Mariadb Node.js 连接器团队的更新日志指出他们有一些错误,其中 Mariadb 没有提供足够的错误消息(它有时只提供“超时”而没有更多信息),所以我改变了我正在寻找的内容,并发现了错误。

对于收到类似错误消息的任何人,这可能意味着任何事情,因此请检查代码的所有部分!

原帖

我正在尝试熟悉 Nodejs 并表达,但遇到了一个我似乎无法解决的问题:

在单独的文件中创建 Mariadb 数据库池并使用 module.exports 导出池时,我无法在另一个文件中使用相同的池。尝试使用池查询数据库时出现超时错误。

如果我在同一个文件中使用完全相同的代码而不是两个单独的文件,则查询可以完美运行,所以我认为在 module.exports = pool 期间出现了问题。

我错过了什么吗?提前致谢!

我有两个文件:index.js


// import express web framework
const express = require("express");

//create an express application
const app = express();

const pool = require('./database/connection')
const cors = require('cors');

//middleware
app.use(cors())
app.use(express.json())

getData = async () => {
    data = await pool.query("call stored_procedure")
    console.log (data)
}

getData()

app.listen(3001, () => {
    console.log('Serving running on port 3001')
})

连接.js

//import mariadb library
const mariadb = require("mariadb")

//function that create mariadb connection pool for database
const createPool = () => {
    try {
        return (
            mariadb.createPool({
                connectionLimit: 10,
                host: process.env.MARIADB_HOST,
                user: process.env.MARIADB_USER,
                password: process.env.MARIADB_PASSWORD,
                database: process.env.MARIADB_DB,
                port: 3306
            })
        )
    }
    catch (err) {
        console.error('Failed to connect to database: ')
        console.error(err)
    }
}

const pool = createPool()

//export database connection pool
module.exports = pool

运行此应用程序会导致以下错误(一段时间后):

path_to_dir/node_modules/mariadb/lib/misc/errors.js:57
  return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
         ^

SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10001ms
    (pool connections: active=0 idle=0 limit=10)
    at Object.module.exports.createError (path_to_dir/node_modules/mariadb/lib/misc/errors.js:57:10)
    at Pool._requestTimeoutHandler (path_to_dir/node_modules/mariadb/lib/pool.js:345:26)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  text: 'retrieve connection from pool timeout after 10001ms\n' +
    '    (pool connections: active=0 idle=0 limit=10)',
  sql: null,
  fatal: false,
  errno: 45028,
  sqlState: 'HY000',
  code: 'ER_GET_CONNECTION_TIMEOUT'
}

【问题讨论】:

    标签: javascript node.js database express mariadb


    【解决方案1】:

    我发现了错误。错误非常明显:我没有包含 require("dotenv").config();在 connection.js 文件中。如果没有这个,数据库连接会在超时后简单地失败,因为它没有任何连接细节。我发现 Mariadb Node.js 连接器团队的更新日志指出他们有一些错误,其中 Mariadb 没有提供足够的错误消息(它有时只提供“超时”而没有更多信息),所以我改变了我正在寻找的内容,并发现了错误。

    对于收到类似错误消息的任何人,这可能意味着任何事情,因此请检查代码的所有部分!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多