---
title: "What are transactions in IndexedDB and why are they important?"  
description: "What are transactions in IndexedDB and why are they important?"  
author: "ICSM Computer"  
published: 2025-07-02  
updated: 2025-07-02  
canonical: https://www.mindstick.com/interview/34307/what-are-transactions-in-indexeddb-and-why-are-they-important  
category: "database"  
tags: ["database", "indexeddb"]  
reading_time: 4 minutes  

---

# What are transactions in IndexedDB and why are they important?

In **IndexedDB**, a **transaction** is a wrapper around one or more operations that ensures **data integrity** and **consistency**. All **read** and **write** operations must happen inside a transaction.

## Why Are Transactions Important?

- **Atomicity**: All operations in a transaction succeed or fail together.
- **Isolation**: Prevents conflicts from simultaneous operations.
- **Lifetime-controlled**: Transactions auto-close after completing or timing out.
- **Required**: You **must** use a transaction to read/write from object stores.

## Syntax and Usage

```javascript
const tx = db.transaction(['store1', 'store2'], 'readwrite');
const store = tx.objectStore('store1');
store.put({ id: 1, name: 'Alice' });
```

### Parameters:

- **Store(s)**: A string or array of object store names.
- **Mode**: `"readonly"` or `"readwrite"`

## Transaction Example

```javascript
const tx = db.transaction('users', 'readwrite');
const store = tx.objectStore('users');

store.add({ id: 1, name: 'Alice' });
store.put({ id: 1, name: 'Alice Updated' });
store.delete(2); // Optional: delete by id

tx.oncomplete = () => {
    console.log('Transaction complete.');
};

tx.onerror = () => {
    console.error('Transaction failed:', tx.error);
};
```

## Transaction Modes

| Mode | Description |
| --- | --- |
| `readonly` | Can only read data |
| `readwrite` | Can read and write |

## Key Transaction Rules

- Transactions **auto-commit** once all operations finish.
- You **cannot reuse a transaction** once it's completed or errored.
- **Do not do async calls (like fetch)** inside a transaction and expect the transaction to stay open — it **will close immediately**.

## Tip: Multiple Stores in One Transaction

```javascript
const tx = db.transaction(['users', 'orders'], 'readwrite');
const users = tx.objectStore('users');
const orders = tx.objectStore('orders');

users.put({ id: 1, name: "Alice" });
orders.add({ orderId: 101, userId: 1 });
```

This ensures both operations succeed or fail together.

## Summary

- **Transactions are mandatory** for all DB operations.
- Ensure **atomic, consistent changes**.
- Must be used with the correct **mode** and **store name(s)**.
- Automatically manage their lifecycle.

## Answers

### Answer by ICSM Computer

In **IndexedDB**, a **transaction** is a wrapper around one or more operations that ensures **data integrity** and **consistency**. All **read** and **write** operations must happen inside a transaction.

## Why Are Transactions Important?

- **Atomicity**: All operations in a transaction succeed or fail together.
- **Isolation**: Prevents conflicts from simultaneous operations.
- **Lifetime-controlled**: Transactions auto-close after completing or timing out.
- **Required**: You **must** use a transaction to read/write from object stores.

## Syntax and Usage

```javascript
const tx = db.transaction(['store1', 'store2'], 'readwrite');
const store = tx.objectStore('store1');
store.put({ id: 1, name: 'Alice' });
```

### Parameters:

- **Store(s)**: A string or array of object store names.
- **Mode**: `"readonly"` or `"readwrite"`

## Transaction Example

```javascript
const tx = db.transaction('users', 'readwrite');
const store = tx.objectStore('users');

store.add({ id: 1, name: 'Alice' });
store.put({ id: 1, name: 'Alice Updated' });
store.delete(2); // Optional: delete by id

tx.oncomplete = () => {
    console.log('Transaction complete.');
};

tx.onerror = () => {
    console.error('Transaction failed:', tx.error);
};
```

## Transaction Modes

| Mode | Description |
| --- | --- |
| `readonly` | Can only read data |
| `readwrite` | Can read and write |

## Key Transaction Rules

- Transactions **auto-commit** once all operations finish.
- You **cannot reuse a transaction** once it's completed or errored.
- **Do not do async calls (like fetch)** inside a transaction and expect the transaction to stay open — it **will close immediately**.

## Tip: Multiple Stores in One Transaction

```javascript
const tx = db.transaction(['users', 'orders'], 'readwrite');
const users = tx.objectStore('users');
const orders = tx.objectStore('orders');

users.put({ id: 1, name: "Alice" });
orders.add({ orderId: 101, userId: 1 });
```

This ensures both operations succeed or fail together.

## Summary

- **Transactions are mandatory** for all DB operations.
- Ensure **atomic, consistent changes**.
- Must be used with the correct **mode** and **store name(s)**.
- Automatically manage their lifecycle.


---

Original Source: https://www.mindstick.com/interview/34307/what-are-transactions-in-indexeddb-and-why-are-they-important

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
