Executing transactions in a for loop only executes one transaction and finishes
See original GitHub issueI am trying to overload indexDB by inserting lots of 64 base images in a loop. However, the transaction in my loop executes only once. How can this be done?
const initIDB = async () => {
const db = await openDB('db', 1, {
// eslint-disable-next-line no-shadow
upgrade(db) {
db.createObjectStore('tempStore', { keyPath: 'id', autoIncrement: true });
},
});
const tx = db.transaction('tempStore', 'readwrite');
await overloadIDB(tx.store);
await tx.done;
return true;
};
const getRandomArbitrary = (min, max) => Math.random() * (max - min) + min;
const overloadIDB = async (store) => {
const imgurl = "bla.png";
const img64 = await toDataURL(imgurl);
for (let i = 0; i < 10; i++) {
if (i > 0 && i % 100 === 0) console.log('A set done');
try {
const num = Math.round(getRandomArbitrary(1, 1000000));
const data = {
id: num,
img: img64,
};
await new Promise(resolve => resolve(store.add(data)));
} catch (e) {
console.log(e.toString());
console.dir(e);
break;
}
}
};
const toDataURL = url => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.onload = () => {
const reader = new FileReader();
reader.onloadend = () => {
resolve((reader.result));
};
reader.readAsDataURL(xhr.response);
};
xhr.send();
});
Keep getting this error
Failed to execute 'add' on 'IDBObjectStore': The transaction has finished
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
Behavior of a loop within a single TRANSACTION
As for the core question: transactions can use savepoints in such iterations, so that iteration 1-4 are saved (committed) even if iteration 5 ......
Read more >sql server - Are explicit transactions needed in this while loop?
Individual statements -- DML, DDL, etc -- are transactions in themselves. So yes, after each iteration of the loop (technically: after each ...
Read more >Debugging background tasks inside loops and transactions
A connection has an attribute called run_on_commit , which is just a list of tasks that will be added to the queue of...
Read more >"COMMIT" should not be used inside a loop
Further, in general use COMMIT should only be used at the end of a transaction. Code that is not structured to have one...
Read more >for Loop - an overview | ScienceDirect Topics
for loops, like while loops, repeatedly execute a block of code until a ... At the beginning of each iteration, the for loop...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You’ll need to handle cases where something else alters the counter while you’re fetching. Here’s how I’d do it:
Right now you’re creating a single transaction to do the read and the write, but it doesn’t work because you’ve got a fetch in the middle of it.
Instead, do the read in a transaction, then do the fetch, then do the write in a second transaction.
On Mon, 28 Oct 2019, 22:23 Rost Myshkin, notifications@github.com wrote: