mysql2-async
Advanced tools
Comparing version 1.1.6 to 1.1.7
@@ -227,3 +227,3 @@ "use strict"; | ||
async transaction(callback, options) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d; | ||
const conn = await new Promise((resolve, reject) => { | ||
@@ -237,31 +237,33 @@ this.pool.getConnection((err, conn) => { | ||
}); | ||
let retries = (_a = options === null || options === void 0 ? void 0 : options.retries) !== null && _a !== void 0 ? _a : 0; | ||
const db = new Queryable(conn); | ||
try { | ||
await db.execute('START TRANSACTION'); | ||
if ((options === null || options === void 0 ? void 0 : options.lockForRead) || (options === null || options === void 0 ? void 0 : options.lockForWrite)) { | ||
const lockForRead = typeof options.lockForRead === 'string' ? [options.lockForRead] : ((_a = options.lockForRead) !== null && _a !== void 0 ? _a : []); | ||
const lockForWrite = typeof options.lockForWrite === 'string' ? [options.lockForWrite] : ((_b = options.lockForWrite) !== null && _b !== void 0 ? _b : []); | ||
await db.execute(`LOCK TABLES ${(_c = lockForRead.map(t => `${t} READ`).concat(lockForWrite.map(t => `${t} WRITE`)).join(', ')) !== null && _c !== void 0 ? _c : ''}`); | ||
} | ||
try { | ||
const ret = await callback(db); | ||
await db.execute('COMMIT'); | ||
return ret; | ||
} | ||
catch (e) { | ||
await db.execute('ROLLBACK'); | ||
const isDeadlock = e.errno === 1213; | ||
if (isDeadlock && (options === null || options === void 0 ? void 0 : options.retries)) { | ||
// wait a random number of milliseconds to help avoid immediately re-colliding with the other process | ||
await new Promise(resolve => { var _a; return setTimeout(resolve, Math.floor(Math.random() * ((_a = options.retryPause) !== null && _a !== void 0 ? _a : 100))); }); | ||
return await this.transaction(callback, { ...options, retries: options.retries - 1 }); | ||
while (true) { | ||
await db.execute('START TRANSACTION'); | ||
try { | ||
if ((options === null || options === void 0 ? void 0 : options.lockForRead) || (options === null || options === void 0 ? void 0 : options.lockForWrite)) { | ||
const lockForRead = typeof options.lockForRead === 'string' ? [options.lockForRead] : ((_b = options.lockForRead) !== null && _b !== void 0 ? _b : []); | ||
const lockForWrite = typeof options.lockForWrite === 'string' ? [options.lockForWrite] : ((_c = options.lockForWrite) !== null && _c !== void 0 ? _c : []); | ||
await db.execute(`LOCK TABLES ${(_d = lockForRead.map(t => `${t} READ`).concat(lockForWrite.map(t => `${t} WRITE`)).join(', ')) !== null && _d !== void 0 ? _d : ''}`); | ||
} | ||
const ret = await callback(db); | ||
await db.execute('COMMIT'); | ||
return ret; | ||
} | ||
else { | ||
throw e; | ||
catch (e) { | ||
await db.execute('ROLLBACK'); | ||
if (e.errno === 1213 && retries > 0) { // deadlock and we're going to retry | ||
retries--; | ||
// wait a random number of milliseconds to help avoid immediately re-colliding with the other process | ||
await new Promise(resolve => { var _a; return setTimeout(resolve, Math.floor(Math.random() * ((_a = options === null || options === void 0 ? void 0 : options.retryPause) !== null && _a !== void 0 ? _a : 100))); }); | ||
} | ||
else { // not deadlock or we're done retrying | ||
throw e; | ||
} | ||
} | ||
finally { | ||
if ((options === null || options === void 0 ? void 0 : options.lockForRead) || (options === null || options === void 0 ? void 0 : options.lockForWrite) || (options === null || options === void 0 ? void 0 : options.unlockAfter)) | ||
await db.execute('UNLOCK TABLES'); | ||
} | ||
} | ||
finally { | ||
if ((options === null || options === void 0 ? void 0 : options.lockForRead) || (options === null || options === void 0 ? void 0 : options.lockForWrite) || (options === null || options === void 0 ? void 0 : options.unlockAfter)) | ||
await db.execute('UNLOCK TABLES'); | ||
} | ||
} | ||
@@ -268,0 +270,0 @@ finally { |
{ | ||
"name": "mysql2-async", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"description": "A wrapper for mysql2 to add convenience, especially when developing with async/await and async iterables.", | ||
@@ -5,0 +5,0 @@ "exports": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30543
365