Skip to content

Commit

Permalink
Update 058-transactions.mdx (#6422)
Browse files Browse the repository at this point in the history
* Update 058-transactions.mdx

* Update content/200-orm/200-prisma-client/100-queries/058-transactions.mdx

Co-authored-by: Jon Harrell <[email protected]>

---------

Co-authored-by: Jon Harrell <[email protected]>
Co-authored-by: Ankur Datta <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent 67f576c commit 0e30d9d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions content/200-orm/200-prisma-client/100-queries/058-transactions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,36 @@ To avoid transaction write conflicts and deadlocks on a transaction:
}
```

### Using `$transaction` within `Promise.all()`

If you wrap a `$transaction` inside a call to `Promise.all()`, the queries inside the transaction will be executed _serially_ (i.e. one after another):

```ts
await prisma.$transaction(async (prisma) => {
await Promise.all([
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
])
})
```

This may be counterintuitive because `Promise.all()` usually _parallelizes_ the calls passed into it.

The reason for this behaviour is that:
- One transaction means that all queries inside it have to be run on the same connection.
- A database connection can only ever execute one query at a time.
- As one query blocks the connection while it is doing its work, putting a transaction into `Promise.all` effectively means that queries should be ran one after another.



## Dependent writes

Writes are considered **dependent** on each other if:
Expand Down

0 comments on commit 0e30d9d

Please sign in to comment.