【问题标题】:Should I worry to close db connection while connecting cloudant db from nodejs using cloudant module?在使用 cloudant 模块从 nodejs 连接 cloudant db 时,我是否应该担心关闭 db 连接?
【发布时间】:2017-05-06 16:38:55
【问题描述】:

我正在使用 nodejs 中的“cloudant”模块连接到 cloudant(npm install --save cloudant)。下面是启动 db 连接变量然后使用它的代码。

//instantiate a cloudant var with the cloudant url. The url has id, password in it.
cloudant = require('cloudant')(dbCredentials.url);

//set the middleware with the db name
db = cloudant.use(dbCredentials.dbName);

//use the db variable to insert data into the db
function(req){

  db.insert({
     name:req.name,
     age:req.age
   },id,function(err,doc){
         ....
   });
};

使用 db 变量后我是否应该担心关闭连接?这对我来说没有意义,因为我们在这里没有使用任何连接池。对我来说,我们只是用端点、凭据和数据库名称来实例化 db 变量。稍后我们将 cloudant 资源称为 ReST API。我在这里有点困惑,不认为我们需要做任何密切的联系(这实际上意味着除了使 cloudant 变量无效之外什么都没有)。可以请分享任何cmets,无论我是对还是错?提前致谢。

【问题讨论】:

    标签: node.js cloudant dbconnection


    【解决方案1】:

    默认情况下,Cloudant 库使用默认的 Node.js 连接池,因此它将遵守服务器的“保持活动”指令,但您无需担心。只需继续调用 Cloudant 库函数,库就会在需要时建立 HTTP 连接 - 必要时重用现有连接,在其他情况下创建新连接。

    【讨论】:

    • 感谢格林的回答。这解释了我在寻找什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多