0
0
Node.jsframework~20 mins

Connection pooling concept in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Connection Pooling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main benefit of using connection pooling in Node.js?

Connection pooling helps manage database connections efficiently. What is the primary advantage of using connection pooling?

AIt guarantees that only one connection is used for all queries.
BIt encrypts all database queries automatically for security.
CIt reduces the overhead of establishing new connections by reusing existing ones.
DIt automatically scales the database server hardware.
Attempts:
2 left
💡 Hint

Think about what happens when you open and close connections repeatedly.

component_behavior
intermediate
2:00remaining
What happens when all connections in a Node.js pool are busy?

Consider a connection pool with a limited number of connections. What happens if all connections are currently in use and a new query requests a connection?

AThe query fails immediately with a connection error.
BThe new query waits until a connection is released back to the pool.
CThe pool closes one active connection to free up space.
DThe pool automatically creates unlimited new connections.
Attempts:
2 left
💡 Hint

Think about how a waiting line works when resources are limited.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly creates a connection pool using the 'mysql2' package in Node.js?

Choose the code that properly creates a connection pool with a maximum of 5 connections.

A
const mysql = require('mysql2');
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test', connectionLimit: 5 });
B
const mysql = require('mysql2');
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test', maxPoolSize: 5 });
C
import mysql from 'mysql2';
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test', maxConnections: 5 });
D
import mysql from 'mysql2';
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test', connectionLimit: 10 });
Attempts:
2 left
💡 Hint

Check the exact option name for limiting connections in the pool.

🔧 Debug
advanced
2:00remaining
Why does this Node.js connection pool code cause a runtime error?

Consider this code snippet:

const mysql = require('mysql2');
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test' });
pool.query('SELECT 1', (err, results) => {
  if (err) throw err;
  console.log(results);
});
pool.end();
pool.query('SELECT 2', (err, results) => {
  if (err) throw err;
  console.log(results);
});

Why does the second query cause an error?

ABecause the query syntax is invalid and causes a syntax error.
BBecause the callback function is missing error handling.
CBecause the database credentials are missing in the pool configuration.
DBecause the pool was closed with pool.end() before the second query.
Attempts:
2 left
💡 Hint

Think about what pool.end() does to the connection pool.

state_output
expert
3:00remaining
What is the output of this Node.js connection pool code snippet?

Given the following code, what will be printed to the console?

import mysql from 'mysql2/promise';

async function testPool() {
  const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test', connectionLimit: 2 });

  const conn1 = await pool.getConnection();
  const conn2 = await pool.getConnection();

  let errorCaught = false;
  try {
    const conn3 = await pool.getConnection({ timeout: 100 });
  } catch (e) {
    errorCaught = true;
    console.log('Timeout error caught');
  }

  conn1.release();
  conn2.release();
  await pool.end();

  console.log('Error caught:', errorCaught);
}

testPool();
A
Timeout error caught
Error caught: true
B
Timeout error caught
Error caught: false
CError caught: true
DNo output, program crashes with unhandled promise rejection
Attempts:
2 left
💡 Hint

Consider the pool size and what happens when requesting more connections than the limit with a timeout.