TransactionInactiveError:无法执行'添加' on' IDBObjectStore':交易未激活

时间:2015-10-10 14:10:48

标签: indexeddb

在此代码中,当我在控制台中查看store1.add时,我在TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction is not active遇到问题。

如果可能的话,任何人都会帮我提供一些示例代码。

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();
    }
  };

}

2 个答案:

答案 0 :(得分:5)

当控制返回到事件循环时,事务变为非活动状态,并且仅在该事务中的操作的回调中再次处于活动状态。

 ... {
   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.
}

目前还不清楚您希望两笔交易如何互动。三种方法是:

  • 在两个商店的范围内都有一个交易 - 这为您提供了您可能期望的交易完整性。
  • 为每个add()
  • 设置单独的写入事务
  • 记录要添加到数组中的所有数据,然后在光标迭代/第一个事务完成后创建第二个事务以发出所有add()调用。

答案 1 :(得分:1)

乍看之下快速猜测是在store1.add发生的错误发生在靠近结尾的for循环中。 store1并不保证具有您期望的值,因为您在openCursor的结果之后引用它,该结果发生在不同的时钟周期上,这意味着idb引擎有机会关闭它,因为它没有找到任何听众。

从最后的函数中获取store1(onsuccess函数)。