0
0
NestJSframework~10 mins

Transactions in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Transactions
Start Transaction
Execute DB Operations
Check for Errors
Commit
End Transaction
This flow shows how a transaction starts, runs database operations, then commits if all is good or rolls back if errors occur.
Execution Sample
NestJS
await this.dataSource.transaction(async manager => {
  await manager.save(user);
  await manager.save(profile);
});
This code runs two save operations inside a transaction that commits if both succeed or rolls back if any fail.
Execution Table
StepActionOperationResultTransaction State
1Start transactionBegin transactionTransaction startedActive
2Save userInsert user recordUser savedActive
3Save profileInsert profile recordProfile savedActive
4No errors detectedCheck operationsAll successfulActive
5Commit transactionCommit changesTransaction committedCommitted
6End transactionClose transactionTransaction endedCommitted
💡 Transaction ends after commit because all operations succeeded
Variable Tracker
VariableStartAfter Step 2After Step 3Final
transactionStateNoneActiveActiveCommitted
userSavedFalseTrueTrueTrue
profileSavedFalseFalseTrueTrue
Key Moments - 2 Insights
Why does the transaction rollback instead of commit if an error happens?
If any operation inside the transaction fails (not shown in this trace), the flow moves to rollback instead of commit to undo all changes, ensuring data stays consistent. See step 4 where errors are checked.
What does 'transactionState' mean in the variable tracker?
'transactionState' shows if the transaction is active, committed, or rolled back. It starts None, becomes Active when transaction begins, and ends Committed if successful (see execution_table steps 1 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the transaction state after saving the user?
ACommitted
BNone
CActive
DRolled back
💡 Hint
Check the 'Transaction State' column at step 2 in the execution_table
At which step does the transaction commit?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for the 'Commit transaction' action in the execution_table
If saving the profile failed, what would happen to the transaction state?
AIt would stay Active
BIt would rollback
CIt would commit anyway
DIt would end without commit or rollback
💡 Hint
Recall the key moment about rollback on errors and the flow after step 3
Concept Snapshot
Transactions in NestJS:
- Use dataSource.transaction(async manager => {...})
- All DB ops inside run atomically
- If all succeed, transaction commits
- If any fail, transaction rolls back
- Keeps data consistent and safe
Full Transcript
In NestJS, transactions let you group database operations so they all succeed or fail together. The flow starts a transaction, runs operations like saving user and profile, then checks for errors. If no errors, it commits the transaction to save changes permanently. If errors occur, it rolls back to undo all changes. The transaction state changes from None to Active when started, then to Committed if successful. This ensures your data stays consistent and avoids partial updates.