Question

Can we create a transaction inside another transaction in IndexedDB?? I want to export my indexedDB datas to my Sql Server.. For that, I'm iterating using cursors and send datas to my Sql Server after the successful insertion, I want to delete the same data in my IDB.. While doing this, I'm getting TransactionInactive Error: Your transaction is not active. Give me some suggestions to solve this..

            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();
                }
            }
Was it helpful?

Solution

Thanks for the question.

You get the TransactionInactiveError because the delete operation you want to preform happens in a callback of the ajax call.

The indexeddb is autocommitting, meaning once the actions on the transaction are done, the transaction commits. Meaning in your case, once all results in the cursor are iterated, the transaction will be commited. The transaction doesn't wait on possible extra calls that can be made due an async callback.

So what you should do is delete the item immediately or open an new transaction for every delete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top