Bird
0
0

Given the following code snippet in a NestJS service using TypeORM, what will be the output if the second operation fails?

medium📝 component behavior Q13 of 15
NestJS - Database with TypeORM
Given the following code snippet in a NestJS service using TypeORM, what will be the output if the second operation fails?
await dataSource.transaction(async (manager) => {
  await manager.save(user1);
  await manager.save(user2); // throws error
});
console.log('Transaction complete');
ANeither user1 nor user2 are saved, and an error is thrown before logging
BOnly user1 is saved, then 'Transaction complete' is logged
CBoth user1 and user2 are saved, then 'Transaction complete' is logged
D'Transaction complete' is logged before any save operations
Step-by-Step Solution
Solution:
  1. Step 1: Understand transaction rollback behavior

    If any operation inside the transaction fails, all changes are rolled back, so no data is saved.
  2. Step 2: Analyze the code flow

    The second save throws an error, so the transaction aborts and neither user1 nor user2 are saved. The error prevents reaching the console.log.
  3. Final Answer:

    Neither user1 nor user2 are saved, and an error is thrown before logging -> Option A
  4. Quick Check:

    Transaction rollback on error = no saves, error thrown [OK]
Quick Trick: Error inside transaction rolls back all changes [OK]
Common Mistakes:
  • Assuming partial saves happen before error
  • Thinking console.log runs despite error
  • Believing transaction commits despite failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes