mssql-cr-layer 3.0.12 → 3.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +21 -7
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const debug = require('debug')('layer:mssql')
|
|
|
5
5
|
const scanMarkers = require('./scan-markers')
|
|
6
6
|
|
|
7
7
|
const connectionParams = new WeakMap() // Hidden connection parameters
|
|
8
|
+
const rolledBack = new WeakMap() // transaction -> rolled back (by us or by the server)
|
|
8
9
|
|
|
9
10
|
// Tag a DB error with the SQL + params that produced it, so error-alert
|
|
10
11
|
// consumers can show what actually failed (drivers report code/number/state but
|
|
@@ -99,9 +100,9 @@ MssqlCrLayer.prototype.transaction = function(fn, options) {
|
|
|
99
100
|
const isolationLevel = options.ISOLATION_LEVEL || this.ISOLATION_LEVEL
|
|
100
101
|
return this.connect().then(function(connection) {
|
|
101
102
|
const transaction = new mssql.Transaction(connection)
|
|
102
|
-
|
|
103
|
+
rolledBack.set(transaction, false)
|
|
103
104
|
transaction.on('rollback', function() {
|
|
104
|
-
rolledBack
|
|
105
|
+
rolledBack.set(transaction, true)
|
|
105
106
|
})
|
|
106
107
|
return transaction
|
|
107
108
|
.begin(mssql.ISOLATION_LEVEL[isolationLevel])
|
|
@@ -114,7 +115,7 @@ MssqlCrLayer.prototype.transaction = function(fn, options) {
|
|
|
114
115
|
})
|
|
115
116
|
})
|
|
116
117
|
.catch(function(err) {
|
|
117
|
-
if (!rolledBack) {
|
|
118
|
+
if (!rolledBack.get(transaction)) {
|
|
118
119
|
return transaction.rollback().then(function() {
|
|
119
120
|
throw err
|
|
120
121
|
})
|
|
@@ -124,8 +125,6 @@ MssqlCrLayer.prototype.transaction = function(fn, options) {
|
|
|
124
125
|
})
|
|
125
126
|
}
|
|
126
127
|
|
|
127
|
-
const rolledBack = new WeakMap()
|
|
128
|
-
|
|
129
128
|
MssqlCrLayer.prototype.beginTransaction = function(options) {
|
|
130
129
|
options = options || {}
|
|
131
130
|
const isolationLevel = options.ISOLATION_LEVEL || this.ISOLATION_LEVEL
|
|
@@ -288,9 +287,24 @@ MssqlCrLayer.prototype.query = function(statement, params, options) {
|
|
|
288
287
|
})
|
|
289
288
|
})
|
|
290
289
|
.catch(function(error) {
|
|
291
|
-
|
|
290
|
+
// The server rolled the transaction back while this statement ran
|
|
291
|
+
// (an error inside a trigger, XACT_ABORT, a trigger ROLLBACK):
|
|
292
|
+
// node-mssql has already handed the connection back to the pool,
|
|
293
|
+
// so it is no longer this statement's to unprepare on - another
|
|
294
|
+
// request may be using it, and the sp_unprepare would collide
|
|
295
|
+
// with it ("Requests can only be made in the LoggedIn state").
|
|
296
|
+
if (transaction && rolledBack.get(transaction)) {
|
|
292
297
|
throw error
|
|
293
|
-
}
|
|
298
|
+
}
|
|
299
|
+
// Never let a failed unprepare replace the statement's own error
|
|
300
|
+
return ps.unprepare().then(
|
|
301
|
+
function() {
|
|
302
|
+
throw error
|
|
303
|
+
},
|
|
304
|
+
function() {
|
|
305
|
+
throw error
|
|
306
|
+
}
|
|
307
|
+
)
|
|
294
308
|
})
|
|
295
309
|
})
|
|
296
310
|
.catch(err => {
|