What is a transaction in the context of databases?
A transaction is a sequence of operations performed as a single logical unit of work. It ensures that either all operations succeed together or none do, keeping data consistent.
Click to reveal answer
beginner
Name the four key properties of transactions (ACID).
Atomicity: All or nothing. Consistency: Data stays valid. Isolation: Transactions don’t interfere. Durability: Changes are permanent once committed.
Click to reveal answer
intermediate
How do you start a transaction using Node.js with a database client like 'pg' for PostgreSQL?
You call client.query('BEGIN') to start a transaction before running your queries.
Click to reveal answer
beginner
What should you do if an error occurs during a transaction?
You should call client.query('ROLLBACK') to undo all changes made during the transaction to keep data consistent.
Click to reveal answer
beginner
Why is transaction handling important in web applications?
It prevents partial updates that can cause data errors, especially when multiple users or processes access the database at the same time.
Click to reveal answer
Which command starts a transaction in SQL?
ABEGIN
BCOMMIT
CROLLBACK
DSAVEPOINT
✗ Incorrect
The BEGIN command starts a transaction block.
What does the COMMIT command do in a transaction?
ASaves all changes permanently
BUndoes all changes
CStarts a new transaction
DLocks the database
✗ Incorrect
COMMIT saves all changes made during the transaction permanently.
In Node.js, which method is used to undo changes if a transaction fails?
Aclient.query('BEGIN')
Bclient.query('COMMIT')
Cclient.query('ROLLBACK')
Dclient.query('SAVEPOINT')
✗ Incorrect
ROLLBACK undoes all changes made during the transaction.
Which ACID property ensures that transactions do not interfere with each other?
ADurability
BIsolation
CAtomicity
DConsistency
✗ Incorrect
Isolation ensures transactions run independently without interference.
Why should you use transactions when multiple users update the same data?
ATo speed up queries
BTo reduce server memory
CTo avoid using indexes
DTo prevent partial updates and keep data consistent
✗ Incorrect
Transactions prevent partial updates that can cause inconsistent data.
Explain how you would handle a transaction in Node.js using a database client.
Think about the sequence of commands to keep data safe.
You got /4 concepts.
Describe the ACID properties and why they matter for transaction handling.
These properties keep your data safe and reliable.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using transactions in Node.js database operations?
easy
A. To ensure multiple database actions succeed or fail together
B. To speed up database queries
C. To create multiple database connections
D. To log database errors automatically
Solution
Step 1: Understand the role of transactions
Transactions group several database actions so they either all succeed or all fail, maintaining data integrity.
Step 2: Compare options
Only To ensure multiple database actions succeed or fail together correctly describes this purpose. Other options describe unrelated features.
Final Answer:
To ensure multiple database actions succeed or fail together -> Option A
Quick Check:
Transaction purpose = group actions for success/failure [OK]
Hint: Transactions keep multiple DB actions atomic and consistent [OK]
Common Mistakes:
Thinking transactions speed up queries
Confusing transactions with connection pooling
Assuming transactions auto-log errors
2. Which of the following is the correct way to start a transaction using a Node.js database client?
easy
A. client.query('START TRANSACTION')
B. client.query('OPEN TRANSACTION')
C. client.query('BEGIN')
D. client.query('INIT TRANSACTION')
Solution
Step 1: Recall standard SQL transaction commands
The standard command to start a transaction is 'BEGIN'.
Step 2: Validate options
Only client.query('BEGIN') uses the correct SQL command 'BEGIN'. Other options are invalid SQL commands.
Final Answer:
client.query('BEGIN') -> Option C
Quick Check:
Start transaction command = BEGIN [OK]
Hint: Use 'BEGIN' to start transactions in SQL [OK]
Common Mistakes:
Using non-standard commands like 'START TRANSACTION'
Confusing transaction start with connection open
Using commands that don't exist in SQL
3. Consider this Node.js code snippet using a PostgreSQL client:
The code correctly uses try-catch to rollback on error.
Step 2: Identify missing error handling for rollback
If the ROLLBACK query itself fails, this code does not catch that error, which can cause unhandled exceptions.
Final Answer:
No error handling for ROLLBACK failure -> Option B
Quick Check:
Rollback errors need separate handling [OK]
Hint: Always handle errors from ROLLBACK queries too [OK]
Common Mistakes:
Assuming ROLLBACK never fails
Placing BEGIN after queries
Putting COMMIT inside catch block
5. You want to perform two related database inserts in Node.js using transactions. Which pattern correctly ensures both inserts succeed or both fail, releasing the client connection properly?