0
0
Node.jsframework~10 mins

Connection pooling concept 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 import the connection pool module.

Node.js
const { Pool } = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bpg
Cfs
Dhttp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'pg' for database connections.
Using 'fs' which is for file system operations.
2fill in blank
medium

Complete the code to create a new connection pool with a max of 10 connections.

Node.js
const pool = new Pool({ max: [1] });
Drag options to blanks, or click blank then click option'
A5
B20
C1
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Setting max to 1 which limits concurrency.
Using a very high number like 20 unnecessarily.
3fill in blank
hard

Fix the error in releasing the client back to the pool.

Node.js
client.[1]();
Drag options to blanks, or click blank then click option'
Arelease
Bend
Cclose
Ddisconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' which closes the connection instead of releasing it.
Using 'end' which terminates the client.
4fill in blank
hard

Fill both blanks to query the database and release the client properly.

Node.js
pool.connect().then(client => {
  return client.query([1])
    .then(res => {
      client.[2]();
      return res;
    });
});
Drag options to blanks, or click blank then click option'
A"SELECT * FROM users"
B"DELETE FROM users"
Crelease
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using DELETE query which removes data instead of fetching.
Calling 'close' instead of 'release' on the client.
5fill in blank
hard

Fill all three blanks to create a pool, query data, and handle errors properly.

Node.js
const pool = new Pool({ max: [1] });

async function fetchData() {
  const client = await pool.connect();
  try {
    const result = await client.query([2]);
    return result.rows;
  } catch (err) {
    console.error([3]);
  } finally {
    client.release();
  }
}
Drag options to blanks, or click blank then click option'
A5
B"SELECT * FROM products"
Cerr
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Setting max to 5 which is lower than intended.
Logging a wrong variable name instead of the error.
Using a wrong query string.