0
0
Node.jsframework~10 mins

Transaction handling in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a transaction using a Node.js database client.

Node.js
const client = await pool.connect();
try {
  await client.query('[1]');
  // perform queries
} finally {
  client.release();
}
Drag options to blanks, or click blank then click option'
AbeginTransaction
BBEGIN
CstartTransaction
DinitTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'beginTransaction' which is not the correct command name in this client.
Using 'startTransaction' or 'initTransaction' which do not exist.
2fill in blank
medium

Complete the code to commit the transaction after successful queries.

Node.js
try {
  await client.query('INSERT INTO users(name) VALUES($1)', ['Alice']);
  await client.query('[1]');
} catch (e) {
  await client.query('ROLLBACK');
}
Drag options to blanks, or click blank then click option'
ACOMMIT
Bfinalize
Csave
DcommitTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'commitTransaction' which is not a command in this context.
Using 'save' or 'finalize' which are not valid transaction commands.
3fill in blank
hard

Fix the error in the code to rollback the transaction on error.

Node.js
try {
  await client.query('UPDATE accounts SET balance = balance - 100 WHERE id = $1', [accountId]);
  await client.query('UPDATE accounts SET balance = balance + 100 WHERE id = $2', [recipientId]);
  await client.query('COMMIT');
} catch (e) {
  await client.query('[1]');
}
Drag options to blanks, or click blank then click option'
Acancel
BrollbackTransaction
CROLLBACK
Dundo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rollbackTransaction' which is not a command in this client.
Using 'cancel' or 'undo' which are not valid transaction commands.
4fill in blank
hard

Fill both blanks to correctly handle transaction with async/await.

Node.js
async function transferFunds(client, fromId, toId, amount) {
  try {
    await client.query('[1]');
    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('[2]');
  } catch (error) {
    await client.query('ROLLBACK');
    throw error;
  }
}
Drag options to blanks, or click blank then click option'
ABEGIN
BCOMMIT
Cstart
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'end' which are not valid transaction commands.
Not committing after beginning the transaction.
5fill in blank
hard

Fill all three blanks to create a transaction that rolls back on error and releases the client.

Node.js
const client = await pool.connect();
try {
  await client.query('[1]');
  await client.query('DELETE FROM orders WHERE id = $1', [orderId]);
  await client.query('[2]');
} catch (err) {
  await client.query('[3]');
  throw err;
} finally {
  client.release();
}
Drag options to blanks, or click blank then click option'
ABEGIN
BCOMMIT
CROLLBACK
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'BEGIN' to start the transaction.
Not rolling back on error.
Not committing after successful queries.