为何node-firebird仅在连续执行大量查询时出现密码错误?
解决node-firebird连续大量查询时的认证错误问题
问题场景
在Windows系统使用node-firebird连接Firebird 4.0数据库,模拟连续多查询时:
- 循环次数较少(如3次)时执行正常
- 增加循环次数后,抛出错误:
Error: Your user name and password are not defined. Ask your database administrator to set up a Firebird login
测试环境准备(isql创建数据库)
SQL> create database 'temp.fdb' page_size 8192 user SYSDBA password 'masterkey'; SQL> create table stuff( id int not null primary key, val int); SQL> insert into stuff values(0, 0);
原测试代码
var Firebird = require('node-firebird'); for (let i = 0; i < 10; ++i) { console.log('running ' + i) var options = {}; options.host = '127.0.0.1'; options.port = 3050; options.database = '<path to your database>'; options.user = 'SYSDBA'; options.password = 'masterkey'; options.retryConnectionInterval = 1000; options.blobAsText = false; Firebird.attach(options, function(err, db) { if (err) throw err; db.query('SELECT * FROM stuff', function(err, result) { if (err) { throw err; } db.detach(); console.log('connection closed: ' + i); }); }); }
错误输出示例
running 0 running 1 running 2 running 3 running 4 running 5 running 6 running 7 running 8 running 9 C:<...>\node-firebird\lib\index.js:3454 throw err; ^ Error: Your user name and password are not defined. Ask your database administrator to set up a Firebird login at doCallback (C:<...>\node_modules\node-firebird\lib\index.js:1360:21) at C:<...>\node_modules\node-firebird\lib\index.js:3138:21 at loop (C:<...>\node_modules\node-firebird\lib\index.js:3198:29) at parseOpResponse (C:<...>\node_modules\node-firebird\lib\index.js:3481:29) at decodeResponse (C:<...>\node_modules\node-firebird\lib\index.js:3203:24) at Socket.<anonymous> (C:<...>\node_modules\node-firebird\lib\index.js:3115:13) at Socket.emit (node:events:512:28) at addChunk (node:internal/streams/readable:343:12) at readableAddChunk (node:internal/streams/readable:316:9) at Readable.push (node:internal/streams/readable:253:10) { gdscode: 335544472, gdsparams: undefined } Node.js v20.4.0
问题原因
该错误并非真的用户名/密码无效,而是短时间内发起的并发连接数超过了Firebird的默认连接限制。Firebird(尤其是Classic/SuperClassic模式)对同时存在的连接数有默认上限,当快速创建大量连接时,数据库无法及时处理所有认证请求,导致返回异常的认证错误。
解决方法
1. 使用连接池复用连接(推荐)
node-firebird支持连接池,通过复用已建立的连接,避免短时间内创建大量新连接,从根本上解决并发限制问题。
修改后的代码示例(使用连接池):
const Firebird = require('node-firebird'); // 创建连接池,设置最大连接数为5 const pool = Firebird.pool(5, { host: '127.0.0.1', port: 3050, database: '<path to your database>', user: 'SYSDBA', password: 'masterkey', retryConnectionInterval: 1000, blobAsText: false }); // 批量执行查询 async function runQueries(count) { const promises = []; for (let i = 0; i < count; ++i) { promises.push(new Promise((resolve, reject) => { console.log('running ' + i); pool.get((err, db) => { if (err) return reject(err); db.query('SELECT * FROM stuff', (err, result) => { if (err) { db.detach(); return reject(err); } db.detach(); // 释放连接回池 console.log('connection released: ' + i); resolve(result); }); }); })); } await Promise.all(promises); pool.destroy(); // 执行完毕后关闭连接池 } // 执行10次查询 runQueries(10).catch(err => console.error(err));
2. 控制并发请求数量
如果不使用连接池,可以限制同一时间的并发连接数,避免超过Firebird的上限。比如使用串行执行:
示例(串行执行):
const Firebird = require('node-firebird'); const options = { host: '127.0.0.1', port: 3050, database: '<path to your database>', user: 'SYSDBA', password: 'masterkey', retryConnectionInterval: 1000, blobAsText: false }; async function runQuery(i) { console.log('running ' + i); return new Promise((resolve, reject) => { Firebird.attach(options, (err, db) => { if (err) return reject(err); db.query('SELECT * FROM stuff', (err, result) => { if (err) { db.detach(); return reject(err); } db.detach(); console.log('connection closed: ' + i); resolve(result); }); }); }); } async function runQueries(count) { for (let i = 0; i < count; ++i) { await runQuery(i); } } runQueries(10).catch(err => console.error(err));
3. 调整Firebird的连接配置(备选)
如果必须使用大量并发连接,可以修改Firebird的配置文件(firebird.conf,通常位于Firebird安装目录下):
- 调整
MaxConnections参数:增加允许的最大连接数(例如设置为MaxConnections = 50) - 若使用SuperServer模式,可调整
ThreadCount等参数优化线程资源
注意:修改配置后需要重启Firebird服务生效,且需考虑Windows系统的资源承载能力,不推荐作为首选方案,因为连接池的资源利用率更高。
内容的提问来源于stack exchange,提问作者margalo
相关产品推荐
相关产品推荐

