From 871a118f1939bf084878f29b302ac852bf57403f Mon Sep 17 00:00:00 2001
From: Marco Bonardo <mbonardo@mozilla.com>
Date: Thu, 9 Sep 2021 16:22:48 +0000
Subject: [PATCH] Bug 1727261 - Rename Sqlite.jsm::_hasInProgressTransaction to
 _initiatedTransaction. r=asuth

initiatedTransaction seems less confusing about the fact Sqlite.jsm initiated this
transaction, rather than something else (that may happen with wrapped connections).

Differential Revision: https://phabricator.services.mozilla.com/D124184
---
 toolkit/modules/Sqlite.jsm | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/toolkit/modules/Sqlite.jsm b/toolkit/modules/Sqlite.jsm
index fb784f6700e61..4ba8058bef933 100644
--- a/toolkit/modules/Sqlite.jsm
+++ b/toolkit/modules/Sqlite.jsm
@@ -264,7 +264,8 @@ function ConnectionData(connection, identifier, options = {}) {
       this._dbConn.defaultTransactionType
     );
   }
-  this._hasInProgressTransaction = false;
+  // Tracks whether this instance initiated a transaction.
+  this._initiatedTransaction = false;
   // Manages a chain of transactions promises, so that new transactions
   // always happen in queue to the previous ones.  It never rejects.
   this._transactionQueue = Promise.resolve();
@@ -296,7 +297,7 @@ function ConnectionData(connection, identifier, options = {}) {
       identifier: this._identifier,
       isCloseRequested: this._closeRequested,
       hasDbConn: !!this._dbConn,
-      hasInProgressTransaction: this._hasInProgressTransaction,
+      initiatedTransaction: this._initiatedTransaction,
       pendingStatements: this._pendingStatements.size,
       statementCounter: this._statementCounter,
     })
@@ -620,16 +621,16 @@ ConnectionData.prototype = Object.freeze({
       let transactionPromise = (async () => {
         // At this point we should never have an in progress transaction, since
         // they are enqueued.
-        if (this._hasInProgressTransaction) {
+        if (this._initiatedTransaction) {
           console.error(
             "Unexpected transaction in progress when trying to start a new one."
           );
         }
-        this._hasInProgressTransaction = true;
         try {
           // We catch errors in statement execution to detect nested transactions.
           try {
             await this.execute("BEGIN " + type + " TRANSACTION");
+            this._initiatedTransaction = true;
           } catch (ex) {
             // Unfortunately, if we are wrapping an existing connection, a
             // transaction could have been started by a client of the same
@@ -641,9 +642,6 @@ ConnectionData.prototype = Object.freeze({
                 "A new transaction could not be started cause the wrapped connection had one in progress",
                 ex
               );
-              // Unmark the in progress transaction, since it's managed by
-              // some other non-Sqlite.jsm client.  See the comment above.
-              this._hasInProgressTransaction = false;
             } else {
               this._log.warn(
                 "A transaction was already in progress, likely a nested transaction",
@@ -667,7 +665,7 @@ ConnectionData.prototype = Object.freeze({
             } else {
               this._log.warn("Error during transaction. Rolling back", ex);
               // If we began a transaction, we must rollback it.
-              if (this._hasInProgressTransaction) {
+              if (this._initiatedTransaction) {
                 try {
                   await this.execute("ROLLBACK TRANSACTION");
                 } catch (inner) {
@@ -690,7 +688,7 @@ ConnectionData.prototype = Object.freeze({
           }
 
           // If we began a transaction, we must commit it.
-          if (this._hasInProgressTransaction) {
+          if (this._initiatedTransaction) {
             try {
               await this.execute("COMMIT TRANSACTION");
             } catch (ex) {
@@ -701,7 +699,7 @@ ConnectionData.prototype = Object.freeze({
 
           return result;
         } finally {
-          this._hasInProgressTransaction = false;
+          this._initiatedTransaction = false;
         }
       })();
 
-- 
GitLab