transaction.rollback() fails after sql error generated by msnodesql driver
See original GitHub issueHi,
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)
- Connect to an sql server.
- Begin a transaction.
- Make any sql query resulting in error (for example, violate a db constraint with an insertstatement).
- Inside errorcallback orerrorevent 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:
- Created 9 years ago
- Comments:6

 Top Related Medium Post
Top Related Medium Post Top Related StackOverflow Question
Top Related StackOverflow Question
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
The option to emit
donedidn’t help - https://github.com/TimelordUK/node-sqlserver-v8/issues/183 The transaction just passes and commits - no rollback