IndexedDB transaction lifetime and commit
Quick answer
Transactions group database operations and define their scope and access mode. A successful individual request is not proof that the full transaction committed.
How it works
db.transaction(storeNames, "readwrite") permits writes in the named stores; readonly is the default. Register complete and abort handlers before issuing requests. A transaction can become inactive once control returns to the event loop with no pending work; do not await unrelated network or timer operations inside it. Await completion before announcing durable application success; browser durability policies may vary.
Local examples
NOT RUN here. Run only on a disposable, isolated HTTP(S) test origin. The snippet deletes its dedicated fixture database; never use a production origin.
(async () => {const name="bytemind-v12-fixture";const requestValue=r=>new Promise((resolve,reject)=>{r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});const finished=tx=>new Promise((resolve,reject)=>{tx.oncomplete=resolve;tx.onabort=()=>reject(tx.error||new Error("aborted"));});await requestValue(indexedDB.deleteDatabase(name));const open=indexedDB.open(name,1);open.onupgradeneeded=()=>{const s=open.result.createObjectStore("items",{keyPath:"id"});s.createIndex("byCategory","category");};const db=await requestValue(open);try{const tx=db.transaction("items","readwrite");const done=finished(tx);tx.objectStore("items").put({id:1,category:"a"});tx.objectStore("items").put({id:2,category:"b"});await done;console.log("committed");}finally{db.close();await requestValue(indexedDB.deleteDatabase(name));}})()Expected console output:
committedComplete is awaited for the group of operations.
NOT RUN here. Run only on a disposable, isolated HTTP(S) test origin. The snippet deletes its dedicated fixture database; never use a production origin.
(async () => {const name="bytemind-v12-fixture";const requestValue=r=>new Promise((resolve,reject)=>{r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});const finished=tx=>new Promise((resolve,reject)=>{tx.oncomplete=resolve;tx.onabort=()=>reject(tx.error||new Error("aborted"));});await requestValue(indexedDB.deleteDatabase(name));const open=indexedDB.open(name,1);open.onupgradeneeded=()=>{const s=open.result.createObjectStore("items",{keyPath:"id"});s.createIndex("byCategory","category");};const db=await requestValue(open);try{const tx=db.transaction("items","readonly");try{tx.objectStore("items").put({id:1});}catch(e){console.log(e.name);}}finally{db.close();await requestValue(indexedDB.deleteDatabase(name));}})()Expected console output:
ReadOnlyErrorWrites cannot run in readonly transactions.
Common mistakes
Do not confuse request success with transaction complete, or use readonly for writes.
Guided practice
Write two records in a single transaction and report success only after completion.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
IndexedDB transaction lifetime and commit — Knowledge Check
Apply the concept and reason about the example output.
Which event signals successful transaction completion?
Which transaction mode permits ordinary writes?
Why avoid unrelated network awaits in a transaction?
What is the expected console output of IndexedDB transaction lifetime and commit example 1?
(async () => {const name="bytemind-v12-fixture";const requestValue=r=>new Promise((resolve,reject)=>{r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});const finished=tx=>new Promise((resolve,reject)=>{tx.oncomplete=resolve;tx.onabort=()=>reject(tx.error||new Error("aborted"));});await requestValue(indexedDB.deleteDatabase(name));const open=indexedDB.open(name,1);open.onupgradeneeded=()=>{const s=open.result.createObjectStore("items",{keyPath:"id"});s.createIndex("byCategory","category");};const db=await requestValue(open);try{const tx=db.transaction("items","readwrite");const done=finished(tx);tx.objectStore("items").put({id:1,category:"a"});tx.objectStore("items").put({id:2,category:"b"});await done;console.log("committed");}finally{db.close();await requestValue(indexedDB.deleteDatabase(name));}})()What is the expected console output of IndexedDB transaction lifetime and commit example 2?
(async () => {const name="bytemind-v12-fixture";const requestValue=r=>new Promise((resolve,reject)=>{r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});const finished=tx=>new Promise((resolve,reject)=>{tx.oncomplete=resolve;tx.onabort=()=>reject(tx.error||new Error("aborted"));});await requestValue(indexedDB.deleteDatabase(name));const open=indexedDB.open(name,1);open.onupgradeneeded=()=>{const s=open.result.createObjectStore("items",{keyPath:"id"});s.createIndex("byCategory","category");};const db=await requestValue(open);try{const tx=db.transaction("items","readonly");try{tx.objectStore("items").put({id:1});}catch(e){console.log(e.name);}}finally{db.close();await requestValue(indexedDB.deleteDatabase(name));}})()