From 9e3e680be80f70a446ceb2e106d458dc2a26b843 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 25 Apr 2015 02:11:09 -0400 Subject: [PATCH] Rework DB transaction handling Rollback callbacks weren't being properly called, and some other things were in the wrong place, particularly with nested transactions. --- chrome/content/zotero/xpcom/db.js | 142 ++++++++++++++++-------------- test/tests/dbTest.js | 83 +++++++++++++++++ 2 files changed, 158 insertions(+), 67 deletions(-) create mode 100644 test/tests/dbTest.js diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index 9f386e6e6a..27c65cb6d3 100644 --- a/chrome/content/zotero/xpcom/db.js +++ b/chrome/content/zotero/xpcom/db.js @@ -465,6 +465,15 @@ Zotero.DBConnection.prototype.executeTransaction = Zotero.Promise.coroutine(func } } + try { + var result = yield Zotero.Promise.coroutine(func)(); + } + catch (e) { + Zotero.debug("Rolled back nested async DB transaction", 5); + this._asyncTransactionNestingLevel = 0; + throw e; + } + Zotero.debug("Decreasing async DB transaction level to " + --this._asyncTransactionNestingLevel, 5); return result; @@ -489,78 +498,77 @@ Zotero.DBConnection.prototype.executeTransaction = Zotero.Promise.coroutine(func } } var result = yield conn.executeTransaction(func); - try { - Zotero.debug("Committed async DB transaction", 5); - - // Clear transaction time - if (this._transactionDate) { - this._transactionDate = null; - } - - if (options) { - // Function to run once transaction has been committed but before any - // permanent callbacks - if (options.onCommit) { - this._callbacks.current.commit.push(options.onCommit); - } - this._callbacks.current.rollback = []; - - if (options.vacuumOnCommit) { - Zotero.debug('Vacuuming database'); - yield Zotero.DB.queryAsync('VACUUM'); - } - } - - // Run temporary commit callbacks - var f; - while (f = this._callbacks.current.commit.shift()) { - yield Zotero.Promise.resolve(f()); - } - - // Run commit callbacks - for (var i=0; i