Skip to content

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

@shandyba

Description

@shandyba

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions