Performance: Transaction handling
This affects the responsiveness and throughput of backend operations that modify data, impacting how fast users see updates and how well the server handles concurrent requests.
async function updateUserData(db, userId, data) { const client = await db.connect(); try { await client.query('BEGIN'); await client.query('UPDATE users SET name = $1 WHERE id = $2', [data.name, userId]); await client.query('UPDATE profiles SET bio = $1 WHERE user_id = $2', [data.bio, userId]); await client.query('COMMIT'); } catch (e) { await client.query('ROLLBACK'); throw e; } finally { client.release(); } }
async function updateUserData(db, userId, data) { await db.query('BEGIN'); await db.query(`UPDATE users SET name = '${data.name}' WHERE id = ${userId}`); await db.query(`UPDATE profiles SET bio = '${data.bio}' WHERE user_id = ${userId}`); await db.query('COMMIT'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking transaction | 0 | 0 | 0 | [X] Bad |
| Asynchronous parameterized transaction with connection pooling | 0 | 0 | 0 | [OK] Good |
await client.query('BEGIN');
await client.query('INSERT INTO users(name) VALUES($1)', ['Alice']);
await client.query('COMMIT');
console.log('Transaction complete');
What will be printed if all queries succeed?try {
await client.query('BEGIN');
await client.query('INSERT INTO orders(id) VALUES(1)');
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
console.error('Transaction failed');
}const client = await pool.connect();
try {
await client.query('BEGIN');
await client.query('INSERT INTO accounts(id) VALUES(1)');
await client.query('INSERT INTO profiles(account_id) VALUES(1)');
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}Pass the test and this pattern moves unknown