question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transaction.rollback() fails after sql error generated by msnodesql driver

See original GitHub issue

Hi,

today I spotted a nasty issue with Transaction interface of node-mssql not working as expected under the following conditions.

Driver used: msnodesql (0.2.1)

  1. Connect to an sql server.
  2. Begin a transaction.
  3. Make any sql query resulting in error (for example, violate a db constraint with an insert statement).
  4. Inside error callback or error event listener (if streaming) attempt to rollback a transaction.

Result: Error rolling back transaction with message “TransactionError: Can’t rollback transaction. There is a request in progress.” + all rows affected by that transaction remain locked (last statement should be investigated better). Expected result: transaction gets rolled back correctly.

Diving a bit into this I found a cause, which, unfortunately, seems to be located in msnodesql’s code:

msnodesql, after emitting an error event does not emit done event (as it does for positive execution cases). Thus, mssql has no chance to release a connection utilized by the call causing an sql error and, subsequently, does not mark transaction as _working = false;, which leads to all subsequent attempts to perform queries on that transactions to get queued and transaction operations to always complete with the abovementioned error message.

I was able to workaround the case by applying the following simple patch to msnodesql’s sources:

diff -r 6891fd1dd238 node_modules/msnodesql/lib/sql.js
--- a/node_modules/msnodesql/lib/sql.js     Mon Sep 15 18:19:12 2014 +0300
+++ b/node_modules/msnodesql/lib/sql.js     Tue Sep 16 16:40:27 2014 +0300
@@ -33,6 +33,7 @@
     }
     else if (notify && notify.listeners('error').length > 0) {
         notify.emit('error', err);
+        notify.emit('done');
     }
     else {
         throw new Error(err);

I.e. by emitting a done event after an error one. Unfortunately, I’m not sure this will work correctly in all occasions, but at least I was able to proceed without any noticeable glitches (yet) in my (rather simple) scenario.

I understand that it is very likely that it will be confirmed that this issue resides in msnodesql’s code (and should be addressed there), but, as we all understand, it looks pretty unlikely it will get fixed any soon.

So, I think if workarounding this case on mssql’s side is not an option, mentioning this as a known issue might at least save time for others having this pain.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

5reactions
dhye360commented, Nov 4, 2015

I’m using 2.3.1 and I still see this issue.
Within a transaction a request fails because the field isn’t large enough; but the rollback fails. It does not crash my Node server, so what state is the system in at this point? One of the connection pool is consumed and stuck?

I apologize for the limited information, but any insight on what I should expect with a transaction would help.

Thanks

0reactions
Doc999torcommented, Nov 14, 2020

The option to emit done didn’t help - https://github.com/TimelordUK/node-sqlserver-v8/issues/183 The transaction just passes and commits - no rollback

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server - transactions roll back on error? - Stack Overflow
It is sent by one long string command. If one of the inserts fail, or any part of the command fails, does SQL...
Read more >
Forcing MS SQL Server to rollback on error
When a batch-aborting error occurs, I believe that SQL is reverting to either the start of the batch or the transaction beginning.
Read more >
node-mssql | Microsoft SQL Server client for Node.js
Tedious; Microsoft / Contributors Node V8 Driver for Node.js for SQL Server ... which is called after transaction has rolled back, or an...
Read more >
Errors 6167 and 1213 occur during transaction rollback with ...
Errors 1213 and 6167 occur when attempting to undo a transaction, which requires the deletion of a record stored in DataServer for MS...
Read more >
Mssql2 NPM - npm.io
Microsoft / Contributors Node V8 Driver for Node.js for SQL Server (native ... determining whether to rollback a transaction automatically if any error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found