tedious: Requests can only be made in the LoggedIn state, not the SentClientRequest state (code: 'EINVALIDSTATE')
In my example below, I’m trying to serially execute an array of functions. But the Requests can only be made in the LoggedIn state, so I had to check for that connection.state !== connection.STATE.LOGGED_IN. And as you can see in the Read function below had to put the request back in queue in order to get rid of the error.
{ [RequestError: Requests can only be made in the LoggedIn state, not the SentClientRequest state]
message: 'Requests can only be made in the LoggedIn state, not the SentClientRequest state',
code: 'EINVALIDSTATE' }
Is there a better way of achieving this? I see that a similar issues were brought up before #19 and #355. Is there a recommended way of executing multiple requests on a connection?
Code:
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var async = require('async');
// Create connection to database
var config = {
userName: 'mylogin',
password: 'mypassword',
server: 'localhost',
options: {
// encrypt: true, /*If you are connecting to a Microsoft Azure SQL database, you will need this*/
database: 'testdb'
}
}
var connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on('connect', function(err) {
if (err) {
console.log(err);
}
else {
console.log("Connected");
// Execute all functions in the array serially
async.waterfall([
function Start(callback){
console.log("Starting...");
callback(null, 'Jake', 'United States');
},
function Insert(name, location, callback){
console.log("Inserting into Table...");
request = new Request("INSERT dbo.employees (name, location) OUTPUT INSERTED.id VALUES (@Name, @Location);", function(err){
if (err) {
console.log(err);
}
}
);
request.addParameter('Name', TYPES.NVarChar, name);
request.addParameter('Location', TYPES.NVarChar, location);
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log("Employee id inserted is " + column.value);
}
});
});
// Check how many rows were inserted
request.on('doneInProc', function(rowCount, more) {
console.log(rowCount + ' row(s) inserted');
callback(null, 'Jared');
});
connection.execSql(request);
},
function Read(callback){
// Requests can only be made in the LoggedIn state, so check for that
if (connection.state !== connection.STATE.LOGGED_IN) {
// Put the request back on the dispatcher if connection is not in LoggedIn state
setTimeout(Read, 0, callback);
return;
}
console.log("Reading rows from the Table...");
// Create the request to read from table
request = new Request("SELECT * FROM dbo.employees;", function(err) {
if (err) {
console.log(err);
}
});
// Output the number of rows read
request.on('doneInProc', function (rowCount, more, rows) {
console.log(rowCount + ' row(s) returned');
callback(null);
});
connection.execSql(request);
}],
function Complete(err, result) {
if(err) {
console.log("Error:", err);
}
else {
console.log("Done!");
}
}
)
}
});
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 25 (8 by maintainers)
In case someone finds it helpful, since Google returns this page first for this error-message. It worked for my single-connection-based service to make a Promise-based wrapper for the connection. Not sure it is a good solution in general though, for I’m not an experienced js-developer:
And the query itself can be executed as this:
So getObjList now can be executed any time without checking the connection state.
For people coming here from Google (like I did): tedious-connection-pool offers a great way of combatting this problem. Using
pool.acquire((err, connection) => ...)andconnection.release()is an easy way to prevent concurrency issues. If you don’t want multiple connections, configure the pool with{min: 1, max: 1}to effectively serialize queries.I am posting a full solution using callbacks for this issue. I had the same problem and when looking at google i ended up here. I load tested this code with a script executing 1 http call every second and also half a second with no issues.
@mikebutak you don’t necessarily need a separate connection for each request. You just need one connection handling one request at a time. To handle multiple request on one connection, you need to execute a new request in the callback of the previous one, after it’s finished. For example,
Was able to resolve this, problem was on my side, didn’t realize you need a separate connection for each request
A common reason for this error is that only one query can be executed on a connection at a time. You need to wait until the request callback is executed, either with an error or with the result before making another request. Please visit the FAQ page here
Since there seems to be too many various issues on this forum. The error
Requests can only be made in the LoggedIn state, not the SentClientRequest statecan be caused by a variety of different reasons. If you experience this error and you did not find the FAQ page helpful, please raise your own issue with the following information so that we may address your issue with more precision:Thanks! 😄