IndexedDB中的TransactionInactiveError

时间:2014-03-24 09:11:26

标签: indexeddb

我们可以在IndexedDB中的另一个事务中创建一个事务吗?我想将indexedDB数据导出到我的Sql Server。为此,我在使用游标进行迭代并在成功插入后将数据发送到我的Sql Server,我想在IDB中删除相同的数据。这个,我得到 TransactionInactive错误:您的交易未激活。给我一些建议来解决这个问题。

            var trans = db.transaction(["Applicant"],"readwrite");
            var store = trans.objectStore("Applicant");
            //console.log(store);

            var index = store.index("AM_Applicant_Name");
            var key = IDBKeyRange.lowerBound(0);
            var cursor = index.openCursor(key);
            //console.log(cursor);

            cursor.onsuccess = function(e) {
                var result = e.target.result;
                console.log(result);

                if(result) {
                    alert(result.value["AM_Applicant_Name"]+" "+result.value["AM_Father_Name"] +" "+result.value["id"]);

                    // Code To Save IDB values to Sql Server
                    if (window.XMLHttpRequest) {
                        var sPath = "IndexedDB.aspx?Insert=Y";
                        console.log(sPath);
                        var xhr = new XMLHttpRequest();
                        xhr.open("POST", sPath, true);
                        xhr.onreadystatechange = function() {
                            if (xhr.readyState == 4 && xhr.status == 200) {
                                if (xhr.responseText != "") {

                                }
                                alert(xhr.getResponseHeader("Y-Result")+" and "+xhr.getResponseHeader("Y-Id"));
                                if (xhr.getResponseHeader("Y-Result") == "Y") {  //Check Data Insertion is Success or Not
                                    alert("Moved Successfully.");
                                }
                                if (xhr.getResponseHeader("Y-Id") != "") {  //Check Data Insertion is Success
                                    alert(xhr.getResponseHeader("Y-Id")+"ds");
                                    //Delete From IDB 

                                    store.delete(xhr.getResponseHeader("Y-Id")); **Here I am getting the error**
                                    console.log("Record Deleted From IndexedDB");
                                }
                            }
                        }


                        //Passing Parameters to Server Side Events
                        xhr.setRequestHeader("District_Code", result.value["AM_District_Code"]);
                        xhr.setRequestHeader("Taluk_Code", result.value["AM_Taluk_Code"]);
                        xhr.setRequestHeader("Hobli_Code", result.value["AM_Hobli_Code"]);
                        xhr.setRequestHeader("Village_Code", result.value["AM_Village_Code"]);
                        xhr.setRequestHeader("Habitation_Code", result.value["AM_Habitation_Code"]);
                        xhr.setRequestHeader("Reservation_Code", result.value["AM_Reservation_Code"]);
                        xhr.setRequestHeader("Caste_Code", result.value["AM_Caste_Code"]);
                        xhr.setRequestHeader("Applicant_Name", result.value["AM_Applicant_Name"]);
                        xhr.setRequestHeader("Father_Name", result.value["AM_Father_Name"]);
                        xhr.setRequestHeader("Mobile_No", result.value["AM_Mobile_No"]);
                        xhr.setRequestHeader("Address", result.value["AM_Address"]);
                        xhr.setRequestHeader("Address1", result.value["AM_Address1"]);
                        xhr.setRequestHeader("Address2", result.value["AM_Address2"]);
                        xhr.setRequestHeader("Pin_Code", result.value["AM_Pin_Code"]);
                        xhr.setRequestHeader("Id", result.value["id"]);

                        xhr.send();
                    }

                    result.continue();
                }
            }

1 个答案:

答案 0 :(得分:4)

感谢您提出问题。

您收到TransactionInactiveError,因为您要执行的删除操作发生在ajax调用的回调中。

indexeddb是自动提交的,这意味着一旦完成对事务的操作,事务就会提交。在您的情况下,一旦迭代了游标中的所有结果,就会提交事务。该事务不会等待由于异步回调而可能产生的额外调用。

所以你应该做的是立即删除项目或为每次删除打开一个新的交易。