Transaction rollback

Aalexandr_li5/5/2023
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;
    });
Bbloberenober5/5/2023
No, the transaction is rolled back automatically
Aalexandr_li5/5/2023
sessionId will be undefined or error will propagate further?
Bbloberenober5/5/2023
the await db.transaction call will throw
Bbloberenober5/5/2023
in other words, .transaction will return a rejected Promise
Aalexandr_li5/5/2023
perfect. Maybe it worth to mention this in docs. I didn't find any info about rollback. Thank you!
Bbloberenober5/5/2023
yes, we're currently working on the proper docs website
Bbloberenober5/5/2023
thanks for the feedback!
Aalexandr_li5/5/2023
Thanks for awesome library! Have a great weekends)