Spring data mongodb not closing mongodb connection

2019-01-25 18:00发布

问题:

I am using spring-data-mongodb (1.7.0.RELEASE) with spring-webmvc framework for my web application. I am using basic CRUD functions using mongoRepository but i am not closing mongo connections in my code cause i thought that spring-data-mongodb will close it by itself, But it keeps on opening new connections and not closing them. These too many connections ares crashing my application and i have to restart tomcat again and again (twice a day) to overcome this.

Note: Spring Application & mongod is on same server. This is log just after crashing -

    2015-07-17T01:31:20.068-0400 I NETWORK  [conn3645] end connection 127.0.0.1:55302 (2583 connections now open)
    2015-07-17T01:31:20.071-0400 I NETWORK  [conn1713] end connection 127.0.0.1:48174 (2352 connections now open)
    2015-07-17T01:31:20.072-0400 I NETWORK  [conn2250] end connection 127.0.0.1:51017 (2325 connections now open)
    2015-07-17T01:31:20.072-0400 I NETWORK  [conn2149] end connection 127.0.0.1:50670 (2320 connections now open)

This is log after restarting tomcat

2015-07-17T01:31:29.994-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53599 #3984 (1 connection now open)
2015-07-17T01:31:33.263-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53740 #3985 (2 connections now open)
2015-07-17T01:31:33.580-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53750 #3986 (3 connections now open)
2015-07-17T02:10:06.477-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50086 #3987 (4 connections now open)
2015-07-17T02:10:06.590-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50090 #3988 (5 connections now open)
2015-07-17T02:10:11.682-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50242 #3989 (6 connections now open)
2015-07-17T02:10:11.780-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50244 #3990 (7 connections now open)
2015-07-17T02:10:12.545-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50255 #3991 (8 connections now open)
2015-07-17T02:10:12.605-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50258 #3992 (9 connections now open)
2015-07-17T02:10:13.413-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50299 #3993 (10 connections now open)

it increase whenever i sends request to the app.

And this is the tomcat log just after crash -

Jul 16, 2015 3:59:57 PM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:404)
    at java.net.ServerSocket.implAccept(ServerSocket.java:545)
    at java.net.ServerSocket.accept(ServerSocket.java:513)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)
    at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:222)
    at java.lang.Thread.null(Unknown Source)

its a development server, it have traffic lesser than 10 call per minute.

Someone please suggest how should i close these connections ?

回答1:

The MongoClient maintains a connection pool,You open a Db connection once with MongoClient and reuse it across your application because setting up a new TCP connection is EXPENSIVE timewise and memory wise that's why you reuse connections. Also a new connection will cause a new Thread to be created on MongoDB using memory on the Db as well.

  • point to be noted that there is a race condition in the connectToMongo method. You need to synchronize access to that method to ensure that at most one instance of MongoClient is ever created.


回答2:

Spring won't close the connection automatically. Only when context is closed, it will close the connections.

Have a look at this article.

It is better for you to configure mongo options as per this article

But you have to fine tune these values depending on your application traffic.

<beans>

  <mongo:mongo host="localhost" port="27017">
    <mongo:options connections-per-host="8"
                   threads-allowed-to-block-for-connection-multiplier="4"
                   connect-timeout="1000"
                   max-wait-time="1500}"
                   auto-connect-retry="true"
                   socket-keep-alive="true"
                   socket-timeout="1500"
                   slave-ok="true"
                   write-number="1"
                   write-timeout="0"
                   write-fsync="true"/>
  </mongo:mongo/>

</beans>


回答3:

I've also faced the same issue because of using new instead of autowiring, if you're manually doing so, close connections using

 mongoDBObject.getDB().getMongo().close().


回答4:

Spring-data-mongodb uses connection pooling to reuse the connection, So it wont closes the connection, and reuse it next time. For this you need to create singleton instance of mongo class.

It works fine if you are creating instance of Mono-repository using Spring IOC (using @AutoWired) Cause it returns singleton instance so only few connections at a time. But i was not creating repository instance using @Autowired i was creating them manually by new method, So it was giving me new mongo() instance each time. new mongo instance means new connection to db. And there was no way to close that. Thats why i was having too many opene connections.