Node-mssql: Simple Prepared Statement execute fails with TransactionError: Can't acquire connection for the request. There is another request in progress.

Created on 30 Oct 2018  路  2Comments  路  Source: tediousjs/node-mssql

Can someone help me understand what I am missing here...

~~~
"use strict"
const sql = require('mssql');

const connectionDetails = {
server : "192.168.1.250"
,user : "sa"
,database : "clone"
,password : "oracle"
,options: {
encrypt: false // Use this if you're on Windows Azure

  }
}

async function main() {

let conn
let results
try {

process.on('unhandledRejection', function (err, p) {
  console.log(`Unhandled Rejection:\Error:`);
  console.log(`${err}\n${err.stack}\n`);
})

conn = new sql.ConnectionPool(connectionDetails);
await conn.connect()
await conn.query(`SET QUOTED_IDENTIFIER ON`);

const statement = `if object_id('"dbo"."T3"','U') is NULL create table "dbo"."T3" ("location" varchar(50));`
const results = await conn.query(statement);

const ps = new sql.PreparedStatement(conn);
ps.input('P1',sql.VarChar(50));
ps.prepare(`insert into "dbo"."T2" ("location") values (@P1)`);
resuls = await ps.execute({P1:'Hello World'});

} catch (e) {
console.log('Failed');
console.log(e);
}

conn.close();
}

main();
~~~

Fails with
~~~
C:\Development\YADAMU\MSSQL>node node\T2
Unhandled Rejection:Error:
TransactionError: Can't acquire connection for the request. There is another request in progress.
TransactionError: Can't acquire connection for the request. There is another request in progress.
at PreparedStatement.acquire (C:\Development\YADAMU\MSSQL\nodenode_modules\mssql\libbase.js:440:30)
at Immediate._execute.err (C:\Development\YADAMU\MSSQL\nodenode_modules\mssql\lib\tedious.js:885:19)
at runCallback (timers.js:696:18)
at tryOnImmediate (timers.js:667:5)
at processImmediate (timers.js:649:5)

Failed
{ RequestError: Could not find prepared statement with handle 0.
at handleError (C:\Development\YADAMU\MSSQL\nodenode_modules\mssql\lib\tedious.js:862:15)
at Connection.emit (events.js:182:13)
at Parser. (C:\Development\YADAMU\MSSQL\nodenode_modules\tedious\lib\connection.js:611:16)
at Parser.emit (events.js:182:13)
at Parser. (C:\Development\YADAMU\MSSQL\nodenode_modules\tedious\libtokentoken-stream-parser.js:54:15)
at Parser.emit (events.js:182:13)
at addChunk (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_readable.js:291:12)
at readableAddChunk (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_readable.js:278:11)
at Parser.Readable.push (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_readable.js:245:10)
at Parser.Transform.push (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_transform.js:148:32)
code: 'EREQUEST',
number: 8179,
lineNumber: 1,
state: 4,
class: 16,
serverName: 'OAKTHWAITE-DB',
procName: 'sp_execute',
originalError:
{ Error: Could not find prepared statement with handle 0.
at handleError (C:\Development\YADAMU\MSSQL\nodenode_modules\mssql\lib\tedious.js:860:19)
at Connection.emit (events.js:182:13)
at Parser. (C:\Development\YADAMU\MSSQL\nodenode_modules\tedious\lib\connection.js:611:16)
at Parser.emit (events.js:182:13)
at Parser. (C:\Development\YADAMU\MSSQL\nodenode_modules\tedious\libtokentoken-stream-parser.js:54:15)
at Parser.emit (events.js:182:13)
at addChunk (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_readable.js:291:12)
at readableAddChunk (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_readable.js:278:11)
at Parser.Readable.push (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_readable.js:245:10)
at Parser.Transform.push (C:\Development\YADAMU\MSSQL\nodenode_modules\readable-stream\lib_stream_transform.js:148:32)
info:
{ number: 8179,
state: 4,
class: 16,
message: 'Could not find prepared statement with handle 0.',
serverName: 'OAKTHWAITE-DB',
procName: 'sp_execute',
lineNumber: 1,
name: 'ERROR',
event: 'errorMessage' } },
name: 'RequestError',
precedingErrors: [] }
~~~

This time I am sure I must be missing something really basic.

Most helpful comment

Looks like I was missing some 'awaits'. Why do you always see these things immediate after posting @

All 2 comments

Looks like I was missing some 'awaits'. Why do you always see these things immediate after posting @

Need an await on the ps.prepare() call

Was this page helpful?
0 / 5 - 0 ratings