Repository navigation
multiple request on same connection in tedious #35
Description
//Code starts here
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var config = {
server: '192.168.1.XXX',
userName: 'username',
password: 'password,
options: {
database: 'database'
}
};
module.exports.execute = function (sp, aParam, callback) {
var paramList = [];
paramList = aParam;
console.log('\r\n trying to execute : ' + sp + '\r\n');
if (typeof (connection) === 'undefined') {
console.log('\r\n Creating New Connection \r\n');
connection = new Connection(config);
connection.on('connect', connected);
connection.on('databaseChange', databaseChange);
connection.on('errorMessage', ErrorMessage);
connection.on('end', end);
}
else {
console.log('\r\n executing sp using old connection');
exec(sp);
}
var rowCollection = [];
function connected(err) {
if (err) {
console.log('\r\n Error while Connecting..., sp : ' + sp + ", Error : " + err + '\r\n');
connection.close();
delete connection;
}
else {
console.log('\r\n Connected.. executing sp using new connection : ' + sp + '\r\n');
exec(sp);
}
}
function exec(sql) {
request = new Request(sql, statementComplete, function (err) {
console.log('\r\n Error while executing request : ' + err + '\r\n');
connection.close();
});
request.on('returnValue', function (parameterName, value, metadata) { });
paramList.forEach(function (param) {
if (param['paramType'] === 'Numeric') {param['paramType'] = TYPES.Numeric;}
else if (param['paramType'] === 'BigInt') {param['paramType'] = TYPES.Int;}
else if (param['paramType'] === 'Decimal') {param['paramType'] = TYPES.Decimal;}
else { param['paramType'] = TYPES.VarChar;}
request.addParameter(param['paramName'], param['paramType'], param['paramValue']);
});
paramList = [];
// request.on('columnMetadata', columnMetadata);
request.on('row', row);
// request.on('done', requestDone);
request.on('doneProc', requestDoneProc);
// request.on('doneInProc', requestDoneInProc);
// connection.prepare(request);
connection.callProcedure(request);
}
function row(columns) {
var row = {};
columns.forEach(function (column) {
if (column.isNull) {
} else {
row[column.metadata.colName] = column.value;
}
});
rowCollection.push(row);
}
function statementComplete(err) {
console.log('\r\n Statement Complete ' + '\r\n');
if (err) {
console.log('\r\n Statement failed: ' + err + '\r\n');
}
else { callback(rowCollection); }
}
function end() {
connection.close();
delete connection;
console.log('\r\n Connection Ended ' + '\r\n');
}
function requestDoneProc(rowCount, more, returnStatus) { console.log("\r\n Request Done Proc : " + returnStatus + '\r\n'); }
function requestDoneInProc(rowCount, more) { console.log("\r\n Request Done InProc : " + rowCount + '\r\n'); }
function requestDone(rowCount, more) { console.log("\r\n Request Done : " + rowCount + '\r\n'); }
function columnMetadata(columnsMetadata) { console.log('\r\n column Metadata : ' + columnsMetadata + '\r\n'); }
function infoError(info) {
console.log(info.number + ' : ' + info.message + ' : ' + info.procName + ' : ' + info.state + ' : ' + info.class + ' : ' + info.lineNumber);
}
function ErrorMessage(error) {
console.log('\r\n ErrorMessage : ' + error.number + ' : ' + error.message + ' : ' + error.procName + ' : ' + error.state + ' : ' + error.class + ' : ' + error.lineNumber);
return;
}
function databaseChange(databaseName) {
console.log('\r\n databaseChange : ' + databaseName + '\r\n');
}
};
//Code ends here
here is our one SQLConnect.js file. that we are using to get resultset from SQL database.
when we call multipul request, it give us errro : "Invalid state; requests can only be made in the LoggedIn state, not the SentPrelogin state"
how can we manage this type of situation.
please help us.
Activity
pekim commentedon Jun 4, 2012
There is some documentation at http://pekim.github.com/tedious/api-connection.html that states that this is not supported.
This is also re-iterated at http://pekim.github.com/tedious/api-request.html#function_newRequest .
So you have two choices. You can wait until the Request's callback (which you are not providing in your example code) is called, and make a second request from the callback (or at some time after the callback has been called). Alternatively, if you want to execute the requests in parallel, you will need to create another Connection and use it to execute the second Request. Depending on your requirements, you might want to maintain a pool of Connections.
This isn't the first time that this question has been raised, so I guess I need to make it clearer in the documentation.
sawaza commentedon Feb 2, 2016
Do you tried to mock your's query's?