无法关闭MongoDB中的连接 - Java驱动程序

时间:2013-12-27 18:51:01

标签: mongodb mongodb-java

我正在使用Java驱动程序与MongoDB的许多db集合进行交互。查询基本上是INSERT,DELETE和FETCH文档。除了连接在日志中打开外,一切正常。

以下是日志文件

Fri Dec 27 17:27:28.484 [initandlisten] connection accepted from 127.0.0.1:51018 #125 (41 connections now open)
Fri Dec 27 17:27:29.209 [initandlisten] connection accepted from 127.0.0.1:51020 #126  (42 connections now open)
Fri Dec 27 17:27:30.222 [initandlisten] connection accepted from 127.0.0.1:51021 #127 (43 connections now open)
Fri Dec 27 17:27:30.254 [conn126] end connection 127.0.0.1:51020 (42 connections now open)
Fri Dec 27 17:27:31.492 [conn125] info DFM::findAll(): extent 0:9f000 was empty, skipping ahead. ns:pingpong.SearchResult
Fri Dec 27 17:27:31.516 [conn125] end connection 127.0.0.1:51018 (41 connections now open)
Fri Dec 27 17:27:40.038 [initandlisten] connection accepted from 127.0.0.1:51032 #128 (42 connections now open)
Fri Dec 27 17:27:40.625 [initandlisten] connection accepted from 127.0.0.1:51033 #129 (43 connections now open)
Fri Dec 27 17:27:41.640 [initandlisten] connection accepted from 127.0.0.1:51034 #130 (44 connections now open)
Fri Dec 27 17:27:41.675 [conn129] end connection 127.0.0.1:51033 (43 connections now open)
Fri Dec 27 17:27:43.062 [conn128] end connection 127.0.0.1:51032 (42 connections now open)
Fri Dec 27 17:27:52.595 [initandlisten] connection accepted from 127.0.0.1:51037 #131 (43 connections now open)
Fri Dec 27 17:27:53.275 [initandlisten] connection accepted from 127.0.0.1:51038 #132 (44 connections now open)
Fri Dec 27 17:27:53.601 [conn131] info DFM::findAll(): extent 0:9f000 was empty, skipping ahead. ns:pingpong.SearchResult
Fri Dec 27 17:27:54.330 [initandlisten] connection accepted from 127.0.0.1:51039 #133 (45 connections now open)
Fri Dec 27 17:27:54.375 [conn132] end connection 127.0.0.1:51038 (44 connections now open)
Fri Dec 27 17:27:55.619 [conn131] end connection 127.0.0.1:51037 (43 connections now open)
Fri Dec 27 17:28:07.823 [initandlisten] connection accepted from 127.0.0.1:51042 #134 (44 connections now open)
Fri Dec 27 17:28:08.439 [initandlisten] connection accepted from 127.0.0.1:51043 #135 (45 connections now open)
Fri Dec 27 17:28:09.475 [initandlisten] connection accepted from 127.0.0.1:51044 #136 (46 connections now open)
Fri Dec 27 17:28:09.496 [conn135] info DFM::findAll(): extent 0:24000 was empty, skipping ahead. ns:pingpong.Messages
Fri Dec 27 17:28:09.509 [conn135] end connection 127.0.0.1:51043 (45 connections now open)
Fri Dec 27 17:28:10.838 [conn134] end connection 127.0.0.1:51042 (44 connections now open)
Sat Dec 28 04:10:58.018 [initandlisten] connection accepted from 127.0.0.1:53778 #137 (45 connections now open)
Sat Dec 28 04:11:04.501 [initandlisten] connection accepted from 127.0.0.1:53779 #138 (46 connections now open)
Sat Dec 28 04:11:05.561 [initandlisten] connection accepted from 127.0.0.1:53780 #139 (47 connections now open)
Sat Dec 28 04:11:05.621 [conn138] info DFM::findAll(): extent 0:24000 was empty, skipping ahead. ns:pingpong.Messages
Sat Dec 28 04:11:05.658 [conn138] info DFM::findAll(): extent 0:24000 was empty, skipping ahead. ns:pingpong.Messages
Sat Dec 28 04:11:05.699 [conn138] info DFM::findAll(): extent 0:24000 was empty,  Sat Dec 28 04:11:05.737 [conn138] end connection 127.0.0.1:53779 (46 connections now open)

我在每个INSERT,DELETE和FETCH命令之后使用mongoclient.close()。但仍然是关系是开放的。我错过了什么。以下是删除代码

MongoClient mongoclient;
String mobileID=getMobileID(messageid);     
DBUtils dbUtils=new DBUtils();
mongoclient=dbUtils.connectToDB("pingpong");
dbUtils.deleteMessage(mobileID,messageid);
mongoclient.close();

我错过了什么。请告诉我。

1 个答案:

答案 0 :(得分:-1)

正如JohnnyHK提到的那样,在评论中,这不是最好的方法。

如果您正在构建一个Web应用程序(例如:REST结构),并为您的应用程序使用容器'(例如:Tomcat 8),您应该做的是声明一个像这样的共享MongoClient实例: private static MongoClient mongoClient;

然后你必须设置 servletContextListener 并实现初始化和/或停止上下文时调用的基本两种方法。

见下面的例子。我正在使用mongo java驱动程序3.0.3,tomcat和servlet-api 3.1.0,它作为Maven的依赖项添加

相关性:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

实施例

@Path("/rest")
@WebListener
public class RESTInterface implements ServletContextListener{
    private static MongoClient mongoClient;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent)            {
        System.out.println("initialized");
        try
        {
            ServerAddress serverAddress = new ServerAddress(IP_connectionString,27017); //default port 27017
            mongoClient = new MongoClient(serverAddress,options);
        }
        catch (Exception e)
        {
            ...
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("MongoClient closed");
        mongoClient.close();
    }

    //whatever you want with the mongoClient !
}