0
0
Node.jsframework~20 mins

Transaction handling in Node.js - 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 if a transaction is not committed in Node.js with a database client?

Consider a Node.js application using a database client that supports transactions. If you start a transaction but never commit or rollback, what will be the state of the transaction?

Node.js
async function runTransaction(client) {
  await client.query('BEGIN');
  await client.query('INSERT INTO users(name) VALUES($1)', ['Alice']);
  // No COMMIT or ROLLBACK
}
AThe transaction remains open and locks the affected rows until the connection closes or a timeout occurs.
BThe transaction automatically commits after the last query executes.
CThe transaction automatically rolls back after the last query executes.
DThe database throws an error immediately after the INSERT query.
Attempts:
2 left
💡 Hint

Think about what happens when a transaction is left hanging without a commit or rollback.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses async/await to handle a transaction in Node.js?

Given a database client with async query support, which code snippet correctly starts a transaction, inserts a row, commits, and handles errors?

A
async function saveUser(client, name) {
  try {
    await client.query('BEGIN');
    await client.query('INSERT INTO users(name) VALUES($1)', [name]);
  } catch (e) {
    await client.query('ROLLBACK');
  }
  await client.query('COMMIT');
}
B
async function saveUser(client, name) {
  await client.query('BEGIN');
  try {
    await client.query('INSERT INTO users(name) VALUES($1)', [name]);
  } catch (e) {
    await client.query('COMMIT');
  }
}
C
async function saveUser(client, name) {
  await client.query('BEGIN');
  try {
    await client.query('INSERT INTO users(name) VALUES($1)', [name]);
    await client.query('COMMIT');
  } catch (e) {
    await client.query('ROLLBACK');
    throw e;
  }
}
D
async function saveUser(client, name) {
  await client.query('BEGIN');
  await client.query('INSERT INTO users(name) VALUES($1)', [name]);
  await client.query('COMMIT');
  await client.query('ROLLBACK');
}
Attempts:
2 left
💡 Hint

Remember to commit only if no error occurs, and rollback on error.

🔧 Debug
advanced
2:00remaining
Why does this transaction code cause a deadlock?

Examine the following code snippet. Why might it cause a deadlock when multiple instances run concurrently?

Node.js
async function transferFunds(client, fromId, toId, amount) {
  await client.query('BEGIN');
  await client.query('UPDATE accounts SET balance = balance - $1 WHERE id = $2', [amount, fromId]);
  await client.query('UPDATE accounts SET balance = balance + $1 WHERE id = $2', [amount, toId]);
  await client.query('COMMIT');
}
ABecause the updates on accounts are done in different order by concurrent transactions, causing circular waits.
BBecause the transaction does not use explicit locks, so the database deadlocks automatically.
CBecause the COMMIT statement is missing, so the transaction never ends.
DBecause the amount parameter is not validated, causing invalid balance updates.
Attempts:
2 left
💡 Hint

Think about how concurrent transactions lock rows in different orders.

state_output
advanced
2:00remaining
What is the final balance after this transaction code runs successfully?

Assume account 1 has balance 100, account 2 has balance 50. What are their balances after this transaction completes?

Node.js
async function transfer(client) {
  await client.query('BEGIN');
  await client.query('UPDATE accounts SET balance = balance - 30 WHERE id = 1');
  await client.query('UPDATE accounts SET balance = balance + 30 WHERE id = 2');
  await client.query('COMMIT');
}

// Initial balances: account 1 = 100, account 2 = 50
AAccount 1: 100, Account 2: 50
BAccount 1: 70, Account 2: 50
CAccount 1: 130, Account 2: 20
DAccount 1: 70, Account 2: 80
Attempts:
2 left
💡 Hint

Subtract 30 from account 1 and add 30 to account 2.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the ACID property 'Isolation' in database transactions?

Choose the statement that best explains the 'Isolation' property of transactions.

AIsolation guarantees that once a transaction commits, its changes are permanent even if the system crashes.
BIsolation ensures that transactions appear to run sequentially, preventing concurrent transactions from interfering with each other.
CIsolation means that all operations within a transaction are completed successfully or none at all.
DIsolation allows multiple transactions to share the same data without any locking.
Attempts:
2 left
💡 Hint

Think about how transactions behave when running at the same time.