0
0
Node.jsframework~10 mins

Connection pooling concept in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Connection pooling concept
Start Application
Create Pool with max connections
Request Connection
Is connection available in pool?
NoWait or Queue Request
Yes
Use Connection
Release Connection back to Pool
Next Request uses Released Connection
Application Ends or Pool Closes
This flow shows how an application creates a pool of reusable connections, checks out connections when needed, and returns them to the pool for reuse.
Execution Sample
Node.js
const pool = createPool({ max: 2 });
const conn1 = await pool.getConnection();
const conn2 = await pool.getConnection();
// Third request waits
await pool.releaseConnection(conn1);
const conn3 = await pool.getConnection();
This code creates a pool with 2 connections, gets two connections, waits on a third, then releases one and gets it again.
Execution Table
StepActionPool State (Available/Used)Connection GrantedNotes
1Create pool with max 2Available: 2 / Used: 0NonePool ready with 2 connections
2Get first connectionAvailable: 1 / Used: 1conn1conn1 checked out
3Get second connectionAvailable: 0 / Used: 2conn2conn2 checked out
4Get third connectionAvailable: 0 / Used: 2WaitNo connections free, request waits
5Release conn1Available: 1 / Used: 1Noneconn1 returned to pool
6Get third connection againAvailable: 0 / Used: 2conn3 (same as conn1)conn3 granted after release
7EndAvailable: 0 / Used: 2NonePool still has 2 connections in use
💡 Execution stops as all connections are in use or released; third request waits until a connection is free.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
pool.availableConnections2100100
pool.usedConnections0122122
conn1nullconn1conn1conn1nullnullnull
conn2nullnullconn2conn2conn2conn2conn2
conn3nullnullnullwaitwaitconn1conn1
Key Moments - 2 Insights
Why does the third connection request wait even though the pool exists?
Because the pool has a max of 2 connections and both are in use (see Step 4 in execution_table), so no connection is available until one is released.
Is the third connection a new connection or reused?
It is reused. After conn1 is released (Step 5), the third request gets that same connection (Step 6), showing reuse of connections.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the pool state after Step 3?
AAvailable: 1 / Used: 1
BAvailable: 0 / Used: 2
CAvailable: 2 / Used: 0
DAvailable: 0 / Used: 0
💡 Hint
Check the 'Pool State' column for Step 3 in the execution_table.
At which step does the third connection request stop waiting and get a connection?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for when 'conn3' is assigned a connection in the execution_table.
If the pool max was increased to 3, what would happen at Step 4?
AThird connection granted immediately
BThird connection still waits
CPool closes
DFirst connection is released automatically
💡 Hint
Refer to the pool max size and available connections in the execution_table.
Concept Snapshot
Connection Pooling Concept:
- Create a pool with max connections.
- Request connections from pool.
- If available, grant connection immediately.
- If none available, wait until one is released.
- Released connections return to pool for reuse.
- Improves performance by reusing connections.
Full Transcript
Connection pooling means creating a set number of connections that can be reused. When the application starts, it creates a pool with a maximum number of connections. When a part of the app needs a connection, it asks the pool. If a connection is free, the pool gives it out. If all connections are busy, the request waits until one is released. When done, the connection is returned to the pool to be used again. This saves time and resources by not creating new connections every time.