Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In Node.js database clients like 'pg', the SQL command to start a transaction is BEGIN.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The SQL command to save changes in a transaction is COMMIT.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The correct SQL command to undo changes in a transaction is ROLLBACK.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'end' which are not valid transaction commands.
Not committing after beginning the transaction.
✗ Incorrect
Transactions start with BEGIN and end with COMMIT to save changes.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Start the transaction with BEGIN, save changes with COMMIT, and undo changes with ROLLBACK on error.