为什么 mongoDB 节点驱动程序会创建不需要的连接? [英] Why mongoDB Node driver is creating unwanted connections?

查看:45
本文介绍了为什么 mongoDB 节点驱动程序会创建不需要的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用版本 3.6.0 中的 mongodb nodejs 本机驱动程序建立到数据库的单个 mongodb 连接时,我遇到了一个奇怪的问题.

I am having a kind of strange problem when I trying to establish a single mongodb connection to a db with the mongodb nodejs native driver in the version 3.6.0.

我的想法是在所有客户端会话中只打开一个连接,并在我的快递服务器中的不同路由中重用它,当我第一次点击它时,getDatabase 函数会创建两个连接,然后是它关闭,一个空闲,但是当我使用一条路由时,第二个再次打开(就像一个连接只是留在那里什么也不做).

My idea is to have just one connection opened through all the client session and reuse it in the different routes I have in my express server, when I hit it the first time the getDatabase function it creates two connections and after that one of it closes and one stands idle, but when I use a route, the second get opened again (it is like one connection just stays there and do nothing).

我只想在我的池中打开一个连接.

I just want to have one connection opened in my pool.

如果您看到我正在使用这些选项测试的注释代码,但没有一个对我有用.

If you see the commented code i was testing with those options but none of them worked for me.

Pd:当我将 socketTimeoutMS 设置为 5000 毫秒时,只创建了一个连接,但它每 5000 毫秒自动关闭并重新打开,这很奇怪(即使在我不使用连接).

Pd: when I set the socketTimeoutMS to 5000ms just one connection is created but it auto-closes and reopen each 5000ms, which is weird (it reopen itself even when I don't use the connection).

当我将 useUnifiedTopology 设置为 true 时,所有这些问题都会发生(我无法将其设置为 false,因为已弃用,其他拓扑将在下一个版本的 mdb ndjs 驱动)

All of this problem happen when I set the useUnifiedTopology to true (I can't set it to false because is deprecated and the other topologies will be removed in the next version of mdb ndjs driver)

这是一张有奇怪行为的图片

代码是:

import { MongoClient, Db } from 'mongodb';
import { DB_URI } from '../config/config';

// This mod works as DataBase Singleton

let db: Db;

export const getDataBase = async (id: string) => {
  try {
    if (db) {
      console.log('ALREADY CREATED');
      return db;
    } else {
      console.log('CREATING');
      let client: MongoClient = await MongoClient.connect(`${DB_URI}DB_${id}`, {
        useUnifiedTopology: true,
   /*  minPoolSize: 1,
        maxPoolSize: 1,
        socketTimeoutMS: 180000,
        keepAlive: true,
        maxIdleTimeMS:10000
        useNewUrlParser: true,
        keepAlive: true,
        w: 'majority',
        wtimeout: 5000,
        serverSelectionTimeoutMS: 5000,
        connectTimeoutMS: 8000,
        appname: 'myApp',
        */
      });

      db = client.db();
      return db;
    }
  } catch (error) {
    console.log('DB Connection error', error);
  }
};

推荐答案

驱动程序在内部为每个已知服务器创建一个连接以进行监控.此连接不用于应用程序操作.

The driver internally creates one connection per known server for monitoring purposes. This connection is not used for application operations.

因此,预计您会建立两个连接.

Hence, it is expected that you would get two connections established.

这篇关于为什么 mongoDB 节点驱动程序会创建不需要的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆