$transaction with an array
When passing an array of Prisma requests to the $transaction function, does it execute the entire batch in one transaction?
4 Replies
You selected the bot's instantaneous insights. Let me compile your answer right away!
Yes, when you pass an array of Prisma Client queries to the
$transaction function, all operations in the array are executed within a single database transaction. This means that either all operations succeed together, or if any operation fails, the entire transaction is rolled back—ensuring atomicity and consistency. The operations are executed sequentially in the order they appear in the array. This approach is suitable for independent writes, such as deleting or updating records across different models, where the operations do not depend on each other’s results.
Here’s an example:
In this example, all three operations will either succeed together or fail together as a single atomic transaction. This is useful for scenarios like deleting a user and all their related data in one go. Note that the order of execution is preserved, but the queries themselves are sent to the database one by one within the transaction context, not as a single SQL batch statement. However, from the application's perspective, it is a single transaction and is often billed as a single operation in environments like Prisma Accelerate, which can also improve performance by reducing network round-trips compared to interactive transactions Transactions and batch queries Accelerate FAQ.Did the bot response help?
✅ Great, thanks for letting us know that this solved your question!
Thanks for confirming that this was resolved.