Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction is not active

Tags:

indexeddb

In this code I am facing problem at store1.add when I look in console it says TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction is not active.

Any one help me, if possible give some sample code.

function indexInitialization(){

     var userDetails = indexedDB.open("dynamicServicess");

     userDetails.onupgradeneeded = function(e) {
         console.log("Upgrading...");
         var thisDB = e.target.result;
         var objectStore=thisDB.createObjectStore("servicess",{keyPath: "ID"});
         var objectStore2=thisDB.createObjectStore("businessareas",{keyPath: "ID"});
         var objectStore3=thisDB.createObjectStore("languages",{keyPath: "ID"});
         var objectStore4=thisDB.createObjectStore("rolenames",{keyPath: "ID"});
         objectStore2.createIndex("language", "LANGLOCALE", { unique: false });
         objectStore.createIndex("businessarea", "BUSINESSAREA", { unique: false });
     objectStore.createIndex("rolename", "ROLENAME", { unique: false });
     }

     userDetails.onsuccess = function(e) {
         console.log("Success!");
         db = e.target.result;
         indexedDbObjectCreation();

     }

     userDetails.onerror = function(e) {
         console.log("Error");      
         console.dir(e);
     }
}

function indexedDbObjectCreation(){
 var transaction = db.transaction(["servicess"],"readwrite");
 var transaction1 = db.transaction(["businessareas"],"readwrite");

 var store = transaction.objectStore("servicess");
 var store1 = transaction1.objectStore("businessareas");

 var req = store.clear();
 var req1 = store1.clear();

 for(i=0;i<result.invocationResult.resultSet.length;i++){               
    store.add({ID:i,SERVICENAME:ss.resultSet[i].SERVICENAME,LANGLOCALE:ss.resultSet[i].LANGLOCALE});    
    }

  var index = store.index("businessarea");
  var singleKeyRange = IDBKeyRange.only("en");  

  index.openCursor(singleKeyRange).onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {
    store1.add({ID:cursor.value.ID,SERVICENAME:cursor.value.SERVICENAME});
       // it is not working error:TransactionInactiveError: Failed to execute
        // 'add' on 'IDBObjectStore': The transaction is not active.
    cursor.continue();
    }
  };

}
like image 284
Swamy Avatar asked Oct 10 '15 14:10

Swamy


2 Answers

Transactions become inactive when control returns to the event loop, and are only active again in callbacks from operations within that transaction.

 ... {
   var transaction = db.transaction(...);
   var transaction1 = db.transaction(...);

   var store = transaction.objectStore(...);
   var store1 = transaction1.objectStore(...);

   // Both transactions active here

   var index = store.index(...);
   index.openCursor(...).onsuccess = function(event) {
      // Async callback from objects from 'transaction' so
      // only 'transaction' is active here, not 'transaction1'
   };

   // No work has been scheduled against 'transaction1' here,
   // so it will attempt to commit as control returns to event 
   // loop here.
}

It's unclear how you are expecting the two transactions to interact. Three approaches would be:

  • Have one transaction with both stores in scope - this gives you the transactional integrity you may be expecting.
  • Have a separate write transaction for each add()
  • Record all the data to add into an array, then once the cursor iteration/first transaction is complete create the second transaction to issue all the add() calls.
like image 108
Joshua Bell Avatar answered Sep 29 '22 04:09

Joshua Bell


Quick guess at first glance is the error occurs at store1.add that occurs within the for loop near the end. store1 isn't guaranteed to have the value you expect because you're referencing it after the result of openCursor, which occurs on a different clock tick, meaning that the idb engine has the opportunity to close it because it did not find any listeners.

Get store1 from within the function at the end (the onsuccess function).

like image 26
Josh Avatar answered Sep 29 '22 05:09

Josh