---
title: Batch Write Transactions
slug: best-practices/batch-write-transactions
docTags: 
createdAt: 2023-09-15T20:13:28.243Z
---

Use a write transaction when you want to combine multiple database operations into a single atomic transaction.

# Cross-Collection Operations

The legacy query API methods support atomic operations across collections. For more information, see `ditto.store.write` in your language's API reference.

:::hint{type="warning"}
Cross-collection atomic write transactions are currently unsupported by Ditto Query Language (DQL); development is actively in progress.
:::

# Implementing Global Async Transactions

All write transactions are event-driven, or asynchronous, but locally scoped by default.&#x20;

When you have many documents to write to a peer, you can initiate a transaction in a way that avoids blocking or slowing down the main thread of your app by starting the transaction asynchronously; as in, it can occur concurrently with other Ditto operations.

For example, in Swift, use `DispatchQueue.global`, as follows:

```swift
DispatchQueue.global(qos: .default).async {

    ditto.store.write { transaction in

        let scope = transaction.scoped(toCollectionNamed: "passengers-\(thisFlight)")

       // Loop inside the transaction to avoid writing to database too frequently
        self.passengers.forEach {
            scope.upsert($0.dict)
        }
    }
}
```

## Log Message: Transaction Remains Blocked

If you have a queued write transaction that remains blocked, the following message appears in your logs:

```json
LOG_LEVEL: Waiting for write transaction (elapsed: XXs), originator=User blocked_by=User
```

If you see this message in your Ditto logs, read the [Troubleshooting](docId\:rqb6IUXxQ4UoPlK4Ss7sk) guide for more information about how to debug blocked database transactions.&#x20;
