Error
Error Code: 145

MongoDB Error 145: Pooled Connections Dropped

📦 MongoDB
📋

Description

Error 145, 'Pooled Connections Dropped,' indicates that connections from your application's connection pool to the MongoDB server have been unexpectedly closed or terminated. This typically occurs when the application attempts to reuse a connection that is no longer valid, leading to connection instability and potential application failures.
💬

Error Message

Pooled Connections Dropped
🔍

Known Causes

4 known causes
⚠️
Network Instability or Interruption
Unstable network connectivity or temporary network outages between the application and the MongoDB server can cause active connections to be dropped.
⚠️
MongoDB Server Restart or Shutdown
If the MongoDB server is restarted or unexpectedly shuts down, all existing connections, including those in the connection pool, will be terminated.
⚠️
Idle Connection Timeouts
Connection pool settings or server-side configurations (like `maxIdleTimeMS`) can cause connections to be closed if they remain idle for too long, leading to the pool detecting a dropped connection.
⚠️
Firewall or Proxy Termination
Intermediate network devices such as firewalls or proxies might terminate long-lived or idle TCP connections without warning, affecting the connection pool.
🛠️

Solutions

3 solutions available

1. Increase Network Timeout Settings medium

Adjust network timeout settings on both MongoDB and client applications to prevent premature connection drops.

1
On the MongoDB server, increase the `net.tcp.connectTimeoutMS` and `net.tcp.idleTimeoutMS` values in your MongoDB configuration file (e.g., `mongod.conf`). A good starting point might be to double the default values or set them to a higher number like 60000 (60 seconds) for idle timeout. Restart the `mongod` service after making changes.
net:
  tcp:
    connectTimeoutMS: 10000
    idleTimeoutMS: 60000
2
On your client applications, ensure that the connection pool settings are configured with appropriate timeouts. For example, in Node.js using the official MongoDB driver, you can set `connectTimeoutMS` and `socketTimeoutMS` in your connection string or options object.
const { MongoClient } = require('mongodb');

const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri, {
  serverSelectionTimeoutMS: 5000, // Example: 5 seconds
  connectTimeoutMS: 10000,      // Example: 10 seconds
  socketTimeoutMS: 45000        // Example: 45 seconds
});

async function run() {
  try {
    await client.connect();
    console.log('Connected successfully to server');
  } finally {
    await client.close();
  }
}
run().catch(console.dir);
3
Consult the documentation for your specific MongoDB driver or ORM for details on how to configure connection pool timeouts.

2. Optimize Application's Connection Management medium

Review and refine how your application manages its MongoDB connections to avoid holding them open unnecessarily or creating too many.

1
Analyze your application's code to identify any instances where connections are being opened but not properly closed or returned to the pool. Ensure that `client.close()` or equivalent methods are called in `finally` blocks or when operations are complete.
2
If your application is creating a large number of connections, consider implementing a more efficient connection pooling strategy or limiting the maximum number of connections in your application's configuration.
3
Look for long-running database operations in your application that might be holding connections open for extended periods. Optimize these queries or break them down into smaller, manageable chunks.

3. Investigate Network Infrastructure and Firewalls advanced

Rule out network-related issues, such as firewalls or load balancers, that might be actively closing idle connections.

1
Check any firewalls, security groups, or network appliances between your application servers and your MongoDB servers. These devices often have their own idle connection timeouts that can be shorter than MongoDB's settings. Adjust these timeouts to be longer or ensure they are configured to allow established connections to persist.
2
If you are using load balancers, examine their configuration for any connection draining or idle timeout settings that might be causing connections to be dropped. Ensure the load balancer is configured to properly handle persistent connections or health checks.
3
Perform network diagnostics (e.g., `ping`, `traceroute`, `netstat`) between your application and MongoDB servers to identify any packet loss or unusual latency that could be contributing to connection instability.
🔗

Related Errors

5 related errors