我需要关闭mongodb的连接吗? [英] Do i need to close connection of mongodb?

查看:799
本文介绍了我需要关闭mongodb的连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb的新手. 我会使用mongodb2.6.x.我正在尝试使用Java驱动程序2.12.x版本与mongodb进行连接,而我正在使用MongoClient与mongodb进行连接.每次我执行任何操作时,我都会在mongodb管理部分收到消息,例如连接数已打开,并且每次我在mongodb中执行某些操作时,连接数都会增加.因此,我担心的是应该关闭连接还是不手动关闭连接.如果我应该手动关闭连接,那是什么过程.因为我从中获取Db对象的MongoClient没有提供任何关闭连接的方法.

I am new in mongodb. And i ma using mongodb 2.6.x. I am trying to connect with mongodb using java driver 2.12.x version and for connecting with mongodb i am using MongoClient. Every time i am doing any operation i am getting message in mongodb admin part like the number of connection is open and every time i am doing some crud operation in mongodb the number of connection increases. So, my concern is should i close the connection or not manually. If i should close the connection manually then what is the process. Because MongoClient from which i am getting the Db object is not giving any method to close the connection.

我为获得连接而编写的代码如下:

The code i have written to get the connection is like this:

new MongoClient("localhost", 27017).getDB("test");

从这里我得到了数据库对象.我正在用我的Web应用程序尝试它.

and from here i am getting the DB object. I am trying it with my web application.

如果有人有任何想法,请让我知道我应该手动关闭mongodb连接.如果是,那怎么办?

if anyone have any idea please let me know should i close the mongodb connection manually. If yes then how?

谢谢.

推荐答案

与传统的数据库驱动程序不同,mongodb java驱动程序处理池和连接管理.所以你在这里很安全.但是,您可能希望通过在关闭应用程序时在MongoClient上调用close来添加关闭挂钩.

Unlike traditional database drivers, the mongodb java driver handles the pooling and connection management. So you are safe here. However you may want to add shut-down hooks by calling close on the MongoClient when you shut-down the application.

这篇关于我需要关闭mongodb的连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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