I have the following code:
transaction = db.transaction("myStore", "readwrite");
objStore = transaction.objectStore(myStore);
Index = objStore.index(key);
request = Index.openCursor(field);
request.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
request = objStore.delete(cursor.value.key);
request.onsuccess = function(event) {
console.log("delete successful");
return;
};
The transaction scope is “myStore”.
I can keep referering to the transaction to keep it alive so that I can recursively delete the returned cursor data.
Does this mean that because it’s the same transaction throughout, any problems with the deletion of data rolls-back all deleted transactions?
A transaction ensures that several actions that need to occur to get a consistent state are successful. If one fails, meaning you would get an inconsistent state, it needs to rollback to the original state. With a concrete example. If you transfer money from one account to an other, you can only have a consistent state if the some of both balances are the same. Meaning when a value is redraw from one account, it must be added to the other account to get a consistent state. If one of both fails, the transaction will return to the original state.
So keeping a transaction alive for performance or something else isn’t a good choice. Also in the indexeddb API transactions are auto commited. Meaning when a transaction doesn’t contain any actions, or gets inactive it will commit the changes.
As last point to awnser your question. All deletes performed in the same transaction will rollback if a deletion fails within the same transaction. If you don’t want this behavior, you will need to create multiple transactions.