Are transactions done in a single round trip? #801
-
Are transactions done in a single round trip? If so, how is it possible to execute arbitrary JavaScript in a transaction? While I can map most of Drizzle ORM to SQL in my head, I can't figure out how transactions work. Mainly, I'm concerned about latency if transactions are done in multiple round trips to the database. Consider this example from the docs: const db = drizzle(...)
await db.transaction(async (tx) => {
const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan'));
if (account.balance < 100) { // random JavaScript execution???
await tx.rollback()
return
}
await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
}); It looks like we can do arbitrary JavaScript in the middle of a transaction. So my assumption is that here at least three requests are made to the database:
…and possibly two more to start and end the transaction. I can imagine this being really slow for more complex transactions. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Exactly. Transactions are separate statements with BEGIN, COMMIT, ROLLBACK statements sent to a database For HTTP-based drivers we are going to implement batch API to reduce roundtrips to the database |
Beta Was this translation helpful? Give feedback.
Exactly. Transactions are separate statements with BEGIN, COMMIT, ROLLBACK statements sent to a database
For HTTP-based drivers we are going to implement batch API to reduce roundtrips to the database