Transaction rollback

Hello guys. Kind of dumb question but should I cover body of transaction in try/catch and call rollback explicitly? Here is an example. What will happen some query throws an error in transaction callback?
sessionId = await db.transaction(async (tx) => {
const [{insertedSessionId}] = await tx
.insert(session)
.values({
auctionid: values.auctionId,
auctiondate: values.auctionDate,
sessiondate: values.previewDate,
})
.returning({insertedSessionId: session.sessionid});
await tx.insert(customersession).values({
sessionid: insertedSessionId,
customerid: values.customerId,
isclone: false,
});
return insertedSessionId;
});
sessionId = await db.transaction(async (tx) => {
const [{insertedSessionId}] = await tx
.insert(session)
.values({
auctionid: values.auctionId,
auctiondate: values.auctionDate,
sessiondate: values.previewDate,
})
.returning({insertedSessionId: session.sessionid});
await tx.insert(customersession).values({
sessionid: insertedSessionId,
customerid: values.customerId,
isclone: false,
});
return insertedSessionId;
});
6 Replies
Dan
Dan2y ago
No, the transaction is rolled back automatically
Alex.dev
Alex.devOP2y ago
sessionId will be undefined or error will propagate further?
Dan
Dan2y ago
the await db.transaction call will throw in other words, .transaction will return a rejected Promise
Alex.dev
Alex.devOP2y ago
perfect. Maybe it worth to mention this in docs. I didn't find any info about rollback. Thank you!
Dan
Dan2y ago
yes, we're currently working on the proper docs website thanks for the feedback!
Alex.dev
Alex.devOP2y ago
Thanks for awesome library! Have a great weekends)
Want results from more Discord servers?
Add your server