我正在开发一个应用程序。它必须以平均水平插入记录。约 900 条记录/秒的速率。
为此,我正在尝试构建一个基于 TCP 的控制台应用程序,它侦听使用 Node.JS 作为我的平台连接到服务器的客户端。
我正在触发一个存储过程以将静态文本(在表中)作为参数插入到 SP,但它不工作并且没有捕获任何错误。
谁能帮我解决这个问题?我是 node.js 的新手
我的服务器端应用程序:
var net = require('net');
var fs = require('fs');
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var HOST = '127.0.0.1';
var PORT = 7000;
var timeout = 420000; // msec - *420000
//db conn config
var config = {
server: '192.168.0.57',
userName: 'sa',
password: 'sa',
options: {
database: "NodeJSBenchmark",
}
};
var lg = function(message) {
console.log(message);
};
var server = net.createServer();
var connection = new Connection(config);
server.on('listening', function() {
lg('Server listening on ' + HOST +':'+ PORT);
});
server.on('connection', function(sock) {
sock.setTimeout(timeout, function() {
try {
sock.end();
}
catch(x) {
lg('on end' + x);
}
});
function executeStatement() {
//var storedProcName = '[NodeJSBenchmark].[dbo].[spINSERTsockdata]';
var storedProcName = 'spINSERTsockdata';
var request = new Request(storedProcName, function(err, rowCount) {
if(err){
lg('Error on insert' + err);
}
else {
lg(rowCount + ' rows2');
lg('SP fininshed running..');
}
connection.close();
});
request.addParameter('sockdata', TYPES.VarChar, 'ping');
request.on('doneProc', function(rowCount, more, returnStatus) {
lg('Rows Changed: ' + rowCount + 'Return Stat: ' + returnStatus);
});
connection.callProcedure(request);
}
sock.setNoDelay(true);
//sock.setEncoding('ascii');
// Identify incoming client
sock.name = sock.remoteAddress + ":" + sock.remotePort
sock.on('data', function(data) {
try {
//lg("CONNECTED: "+sock.name);
//lg("INCOMING DATA: "+data);
sock.write(data);
connection.on('connect', function(err) {
// If conn is active; hit db..
executeStatement();
});
}
catch(x) {
lg(x);
}
});
sock.on('end', function(data) {
try {
sock.end();
}
catch(x) {
lg('on end' + x);
}
});
sock.on('error', function(err) {
lg(err);
});
sock.on('close', function(data) {
try {
sock.end();
}
catch(x) {
lg(x);
}
try {
sock.destroy();
}
catch(x) {
lg('on close' + x);
}
});
sock.on('timeout', function() {
});
});
server.on('error', function(err) {
});
server.on('close', function() {
});
server.listen(PORT, HOST);
SP:
USE [NodeJSBenchmark]
GO
/****** Object: StoredProcedure [dbo].[spINSERTsockdata] Script Date: 9/5/2013 4:16:19 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spINSERTsockdata] @sockdata varchar(10)
AS
SET NOCOUNT ON
INSERT INTO [dbo].[tblSockData]
([inbound])
VALUES
(@sockdata)