我正在尝试连接到集群工作者中的 SQL Server 数据库。我使用node-mssql和Tedious作为驱动程序。这是我的测试代码。
'use strict';
var os = require('os');
var numCPUs = os.cpus().length;
var cluster = require('cluster');
var mssql = (cluster.isMaster == false ? require('mssql') : null);
function workerLog(msg) {
process.send({msg:"CLUSTER-NODE-"+cluster.worker.id+": "+msg});
}
if(cluster.isMaster == true) {
console.log("MASTER: "+"SPAWNING "+numCPUs+" CLUSTER NODES");
for(var i = 0; i < numCPUs; i++) {
var worker = cluster.fork();
worker.on('message', function(msg) {
console.log(msg.msg);
});
}
var timeOuts = [];
var workerError = function(workerId) {
console.log("MASTER: "+"AN EXECUTION ISSUE OCCURRED WITH CLUSTER NODE "+workerId);
};
cluster.on('fork', function(worker) {
timeOuts[worker.id] = setTimeout(workerError,10000,worker.id);
console.log("MASTER: "+"SPAWNED CLUSTER NODE "+worker.id);
});
cluster.on('online', function(worker) {
console.log("MASTER: "+"CLUSTER NODE "+worker.id+" COMING ONLINE");
});
cluster.on('listening', function(worker,address) {
clearTimeout(timeOuts[worker.id]);
console.log("MASTER: "+"CLUSTER NODE "+worker.id+" IS LISTENING");
});
cluster.on('disconnect', function(worker) {
console.log("MASTER: "+"CLUSTER NODE "+worker.id+" HAS DISCONNECTED");
});
cluster.on('exit', function(worker,code,signal) {
clearTimeout(timeOuts[worker.id]);
console.log("MASTER: "+"CLUSTER NODE "+worker.id+" HAS EXITED");
});
} else {
var dbName = "Prefs";
var cfg = {
driver: 'tedious',
user: 'webmi',
password: 'webmi01',
server: "localhost\\SQLEXPRESS",
database: dbName,
options: {
useColumnNames: true,
isolationLevel: 1, // READ_UNCOMMITTED
connectionIsolationLevel: 1 // READ_UNCOMMITTED
}
};
var dbConn = new mssql.Connection(cfg);
workerLog("CONNECT TO "+dbName);
dbConn.connect().then(function() {
if(dbConn.connected) {
workerLog("CONNECTION TO "+dbName+" EXISTS");
} else {
workerLog("NOT CONNECTED TO "+dbName+" BUT NO ERROR DETECTED");
}
}).catch(function(error) {
workerLog("CANNOT CONNECT TO DATABASE\n"+error.stack);
});
}
这是我运行它时得到的结果:node test.js
D:\proj\CTech\9.2\Bin\Node>node test.js
MASTER: SPAWNING 2 CLUSTER NODES
MASTER: SPAWNED CLUSTER NODE 1
MASTER: SPAWNED CLUSTER NODE 2
MASTER: CLUSTER NODE 1 COMING ONLINE
MASTER: CLUSTER NODE 2 COMING ONLINE
CLUSTER-NODE-2: CONNECT TO Prefs
CLUSTER-NODE-1: CONNECT TO Prefs
events.js:85
throw er; // Unhandled 'error' event
^
Error: write ENOTSUP
at exports._errnoException (util.js:746:11)
at ChildProcess.target._send (child_process.js:484:28)
at ChildProcess.target.send (child_process.js:416:12)
at sendHelper (cluster.js:676:8)
at send (cluster.js:512:5)
at cluster.js:488:7
at SharedHandle.add (cluster.js:99:3)
at queryServer (cluster.js:480:12)
at Worker.onmessage (cluster.js:438:7)
at ChildProcess.<anonymous> (cluster.js:692:8)
D:\proj\CTech\9.2\Bin\Node>
看着child_process.js
,似乎工作人员正试图将连接句柄发送回主服务器(出于共享目的?)但我可能错了。在任何情况下,工作人员都会在发送尝试时出错。
有没有办法防止这个错误?或者有没有办法让工人不尝试与主人共享这个句柄?