0
0
NestJSframework~20 mins

Transactions in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a NestJS service method uses a transaction but an error occurs?

Consider a NestJS service method that wraps multiple database operations inside a transaction using TypeORM's queryRunner. If one operation fails and throws an error, what is the expected behavior of the transaction?

NestJS
async function performOperations(queryRunner) {
  await queryRunner.startTransaction();
  try {
    await queryRunner.manager.save(entity1);
    await queryRunner.manager.save(entity2);
    throw new Error('Failure');
    await queryRunner.commitTransaction();
  } catch (error) {
    await queryRunner.rollbackTransaction();
    throw error;
  } finally {
    await queryRunner.release();
  }
}
AOnly entity1 is saved; entity2 is not saved due to the error.
BNeither entity1 nor entity2 is saved; the transaction rolls back all changes.
CBoth entity1 and entity2 are saved permanently despite the error.
DThe transaction commits partially, saving entity1 and rolling back entity2.
Attempts:
2 left
💡 Hint

Think about what a transaction does when an error occurs inside it.

📝 Syntax
intermediate
1:30remaining
Which option correctly starts and commits a transaction using TypeORM's QueryRunner in NestJS?

Choose the code snippet that correctly starts a transaction, performs an operation, and commits it using QueryRunner.

Aawait queryRunner.startTransaction(); await queryRunner.manager.save(entity); await queryRunner.commitTransaction(); await queryRunner.release();
Bawait queryRunner.manager.startTransaction(); await queryRunner.save(entity); await queryRunner.commit(); await queryRunner.release();
CqueryRunner.startTransaction(); queryRunner.manager.save(entity); queryRunner.commitTransaction(); queryRunner.release();
Dawait queryRunner.begin(); await queryRunner.manager.save(entity); await queryRunner.commit(); await queryRunner.release();
Attempts:
2 left
💡 Hint

Check the exact method names on QueryRunner for starting and committing transactions.

🔧 Debug
advanced
2:30remaining
Why does this NestJS transaction code cause a 'QueryRunner already released' error?

Review the following code snippet. It throws a 'QueryRunner already released' error when running multiple transactions sequentially. What is the cause?

NestJS
async function runTransaction(data) {
  const queryRunner = dataSource.createQueryRunner();
  await queryRunner.connect();
  await queryRunner.startTransaction();
  try {
    await queryRunner.manager.save(data);
    await queryRunner.commitTransaction();
  } catch (err) {
    await queryRunner.rollbackTransaction();
  } finally {
    await queryRunner.release();
  }
}

await runTransaction(entity1);
await runTransaction(entity2);
AThe queryRunner is released inside the function, so calling runTransaction twice creates conflicts.
BThe queryRunner is not connected before starting the transaction, causing the error.
CThe dataSource is not initialized, so queryRunner cannot be created.
DThe function does not await commitTransaction, so the release happens too early.
Attempts:
2 left
💡 Hint

Consider what happens to the queryRunner after release() is called.

state_output
advanced
3:00remaining
What is the final state of the database after nested transactions with savepoints in NestJS?

Given the following pseudo-code using TypeORM's QueryRunner with savepoints, what data remains saved after execution?

NestJS
await queryRunner.startTransaction();
try {
  await queryRunner.manager.save(entityA);
  await queryRunner.query('SAVEPOINT sp1');
  await queryRunner.manager.save(entityB);
  throw new Error('Error after sp1');
  await queryRunner.query('RELEASE SAVEPOINT sp1');
  await queryRunner.commitTransaction();
} catch (e) {
  await queryRunner.query('ROLLBACK TO SAVEPOINT sp1');
  await queryRunner.commitTransaction();
}
ABoth entityA and entityB are saved permanently.
BOnly entityB is saved; entityA is rolled back.
CNeither entityA nor entityB is saved; the entire transaction is rolled back.
DOnly entityA is saved; entityB is rolled back to the savepoint.
Attempts:
2 left
💡 Hint

Think about what rolling back to a savepoint does inside a transaction.

🧠 Conceptual
expert
2:00remaining
Why should you avoid long-running transactions in NestJS applications?

In NestJS applications using database transactions, why is it important to keep transactions short and avoid long-running ones?

ALong transactions are automatically converted to read-only, preventing data changes.
BLong transactions automatically commit after a timeout, risking partial data saves.
CLong transactions can lock database rows or tables, reducing concurrency and causing performance issues.
DLong transactions consume less memory but increase CPU usage drastically.
Attempts:
2 left
💡 Hint

Consider how databases handle locks during transactions.