tediousjs/tedious-connection-pool

error thrown in tedious-connection-pool when calling release()

mcrummey opened this issue · 2 comments

The following error occurs in connection-pool.js line: 245 resulting in the closing of the connection.

The connection should be reused.

Here is my code:

runStoredProc = function (storedprocedure, parameters) {
    return new Promise((resolve, reject) => {
        const results = [];
        pool.acquire((err, mscon) => {
            if (err) {
                logger.error(err);
            } else {
                const request = new Request(storedprocedure, function (err) {
                    if (err) {
                        logger.error("Request Error" + err);
                        reject(err);
                    }
                });
                _.each(parameters, function (value, key) {
                    request.addParameter(key, value[1], value[0]);
                });
                request.on("doneInProc", function (rowCount, more, rows) {
                    results.push(rows);
                });
                request.on("doneProc", function (rowCount, more, returnStatus, rows) {
                    resolve({
                        returnStatus: returnStatus,
                        rows: results
                    });
                    // process.nextTick(mscon.release.bind(mscon));
                    mscon.release();
                });
                mscon.callProcedure(request);
            }
        });
    });
}

Below is the stack trace.

"{"message":"Requests can only be made in the LoggedIn state, not the SentClientRequest state","stack":"RequestError: Requests can only be made in the LoggedIn state, not the SentClientRequest state
at RequestError (/home/user/dev/services/sqlReader/node_modules/tedious/lib/errors.js:34:12)
at Connection.makeRequest (/home/user/dev/services/sqlReader/node_modules/tedious/lib/connection.js:905:33)
at Connection.execSqlBatch (/home/user/dev/services/sqlReader/node_modules/tedious/lib/connection.js:710:19)
at Connection.reset (/home/user/dev/services/sqlReader/node_modules/tedious/lib/connection.js:941:19)
at ConnectionPool.release (/home/user/dev/services/sqlReader/node_modules/tedious-connection-pool/lib/connection-pool.js:241:24)
at Connection.release (/home/user/dev/services/sqlReader/node_modules/tedious-connection-pool/lib/connection-pool.js:7:15)
at Request. (/home/user/dev/services/sqlReader/src/MSSql.js:85:32)
at emitMany (events.js:127:13)
at Request.emit (events.js:201:7)
at Parser. (/home/user/dev/services/sqlReader/node_modules/tedious/lib/connection.js:345:26)
at emitOne (events.js:96:13)
at Parser.emit (events.js:188:7)
at Parser. (/home/user/dev/services/sqlReader/node_modules/tedious/lib/token/token-stream-parser.js:42:15)
at emitOne (events.js:96:13)
at Parser.emit (events.js:188:7)
at readableAddChunk (/home/user/dev/services/sqlReader/node_modules/tedious/node_modules/readable-stream/lib/_stream_readable.js:213:18)","code":"EINVALIDSTATE"}"

tedious-connection-pool-stacktrace.txt

I can't make sense out of your post. But if I had to guess, I would say you are not waiting for the procedure call to complete before releasing the connection. The connection pool is trying to reset the connection state, but it can't do that while a query is running. The same thing would happen if you released the connection and it was acquired by some other part of your code. You wouldn't be able to use the connection until the stored procedure completed.

If that is not the problem you are having, please properly format your code and embed the stack trace. Then I'll look at it again.

Calling release() in the callback of the new Request() method fixes my issue. Should have double checked the example before creating this issue.

Thanks for your time.