Hey David,
Thanks for sharing your use case. We indeed rely on the database to manage the transactions.
A common reason for using transactions is writing multiple rows to the database atomically. The Prisma Client supports this with:
1.
Nested Writes: write related data together at once inside a transaction.
2.
$transaction API: write unrelated data together at once inside a transaction.
3.
Batch APIs: create, update or delete many records at once.
Each of these techniques use database transactions under the hood.
What we don't allow is opening transactions for unbound periods of time. A situation like the following is _not permitted_:
1. Begin the transaction
2. Read data from database
3. Fetch data from third party API
(could be slow or fail)
4. Merge the data from the database and third party API
5. Write the data to the database
6. Commit the transaction
These long-running transactions (LRT) kill performance for larger applications. This Read-Modify-Write pattern is common in real-world applications, so we need strategies to accomplish the same result without falling back to LRT.
The Prisma Client provides two techniques for solving this pattern, depending on the situation:
1.
Idempotent operations: write your code so the same input has the same output
2.
Optimistic Concurrency Control: versioning to check when the row has changed
This response just scratches the surface. There's a lot more detail about each of these patterns in
The Transaction Guide.