You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoError: server instance pool was destroyed报错求助(附应用代码)

How to Fix "MongoError: server instance pool was destroyed"

Hey there, let's break down this MongoDB connection error you're facing. That MongoError: server instance pool was destroyed usually pops up when your connection pool gets closed before your database operations finish, or when your app is mismanaging connection references. Let's walk through fixes tailored to your code snippet:

1. Fix the Function Name Typo & Pass Active Connections

First, I noticed a small typo in your code: you imported downloaderCoverageIta but tried calling downloadCoverageIta() (missing the "er" at the end). Beyond that, your router functions aren't getting access to the active connection you created—if they're using a stale or separate connection, that's a common trigger for this error.

Update your start function to pass the live connection and collection to your router:

var mongolib = require('./middlewares/db.js');
var downloaderCoverageWho = require('./routers/downloaderCoverageWho.js');
var downloaderCoverageIta = require('./routers/downloaderCoverageIta.js');
const start = async function() {
  const conn = await mongolib.connectToMongoDb();
  const coverages = await mongolib.createACollection('coverages');
  const isPageHasUpdates = true;
  
  if(isPageHasUpdates) {
    // Pass the active connection/collection to your router function
    await downloaderCoverageIta(conn, coverages);
    // Repeat for downloaderCoverageWho if needed
  }

  // Only close the connection AFTER all operations are done!
  // await conn.close(); // Uncomment this only when your app is finished with DB work
};

// Don't forget to catch errors when running your start function
start().catch(err => console.error("App failed to run:", err));

Then adjust your router file (like downloaderCoverageIta.js) to use the passed connection instead of creating a new one:

// Example in downloaderCoverageIta.js
module.exports = async function(activeConn, coveragesCollection) {
  // Use the provided collection for operations
  await coveragesCollection.insertMany(yourData);
  
  // Or access the database via the active connection
  const db = activeConn.db();
  await db.collection('someOtherCollection').updateOne(yourQuery);
};

2. Avoid Premature Connection Closures

Double-check your mongolib.connectToMongoDb() function. Make sure it isn't calling conn.close() or client.close() anywhere inside the function, and that you're not closing the connection in your app before all database operations are finished. The connection pool should stay open for the lifecycle of your app (or until you explicitly shut it down after all work is done).

3. Reuse a Single Connection Pool (Best Practice)

Your mongolib module should create one connection pool once and reuse it across your app, instead of spinning up new connections every time. Here's a robust pattern for that:

// middlewares/db.js
const { MongoClient } = require('mongodb');

let client;
let dbInstance;

async function connectToMongoDb() {
  // Reuse existing connection if it's still active
  if (client && client.isConnected()) {
    return client;
  }
  
  client = await MongoClient.connect('your-mongodb-uri', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });
  dbInstance = client.db('your-db-name');
  return client;
}

function getDb() {
  if (!dbInstance) throw new Error("Database not connected yet!");
  return dbInstance;
}

async function createACollection(collectionName) {
  const db = getDb();
  const collectionExists = await db.listCollections({ name: collectionName }).hasNext();
  
  if (!collectionExists) {
    await db.createCollection(collectionName);
  }
  
  return db.collection(collectionName);
}

module.exports = { connectToMongoDb, getDb, createACollection };

This way, you only establish one connection pool, eliminating issues from multiple conflicting connection attempts.

4. Ensure All Async Operations Are Awaited

Make sure every database call in your routers is properly awaited. If you have unawaited promises, your code might try to close the connection before operations finish, destroying the pool prematurely.


内容的提问来源于stack exchange,提问作者user7558372

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:24:55